ArrayList 抽奖游戏——多余的数组列表
ArrayList lottery game - excess array lists
这个程序我修了好久,还是不知道哪里出了问题。
我的问题是,如果我想获得多于 1 张票,它会给我比预期更多的数组列表。我找不到一个模式,好像我输入2,我得到3,如果我输入3,我得到6,如果我输入4,我得到10.
输入:我想要多少张票的金额。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayListLottery {
public static void main(String[] args) {
int range = 49, amount = -1, number = 0, choice = -1;
// ArrayList<Integer> tickets = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> games = new ArrayList<ArrayList<Integer>>();
do {
System.out.println("Enter amount of lottery tickets you want");
Scanner in = new Scanner(System.in);
if (amount < 0) {
amount = in.nextInt();
}
while (amount != 0) {
System.out.println("Entered while block");
for (int i = 0; i < amount; i++) {
// Create an arraylist for how
// many tickets i want
ArrayList<Integer> tickets = new ArrayList<Integer>();
games.add(tickets);
for (int k = 0; k < 6; k++) { // Limit the size of a ticket to
// 6
if (tickets.size() < 6) {
number = (int) (range * Math.random()) + 1;
tickets.add(number);
Collections.sort(tickets);
} else {
break;
}
}// limit for loop to 6 end
}// arraylist creator end
amount--;
System.out.println("Amount is " + amount);
}//while loop end
for (List<Integer> i : games) { //print out each ticket
for (Integer n : i) {
System.out.print(n + " ");
}
System.out.println();
} //print out for-loop end
games.clear();
System.out.println("Type 0 to exit, otherwise pick any other number ");
choice = in.nextInt();
amount = choice;
} while (amount != 0);
System.out.println("Good luck!");
}
}
Fibonacci number of amount
将是您的游戏列表大小。
这是因为您已经为 amount
.
应用了 while 和 for 循环
用 if
语句替换你的 while
循环。
你可以放
amount--;
在生成arraylist的for循环中。
像这样:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayListLottery {
public static void main(String[] args) {
int range = 49, amount = -1, number = 0, choice = -1;
// ArrayList<Integer> tickets = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> games = new ArrayList<ArrayList<Integer>>();
do {
System.out.println("Enter amount of lottery tickets you want");
Scanner in = new Scanner(System.in);
if (amount < 0) {
amount = in.nextInt();
}
while (amount != 0) {
System.out.println("Entered while block");
for (int i = 0; i < amount; i++) {
// Create an arraylist for how
// many tickets i want
ArrayList<Integer> tickets = new ArrayList<Integer>();
games.add(tickets);
for (int k = 0; k < 6; k++) { // Limit the size of a ticket to
// 6
if (tickets.size() < 6) {
number = (int) (range * Math.random()) + 1;
tickets.add(number);
Collections.sort(tickets);
} else {
break;
}
}// limit for loop to 6 end
amount--;
}// arraylist creator end
System.out.println("Amount is " + amount);
}//while loop end
for (List<Integer> i : games) { //print out each ticket
for (Integer n : i) {
System.out.print(n + " ");
}
System.out.println();
} //print out for-loop end
games.clear();
System.out.println("Type 0 to exit, otherwise pick any other number ");
choice = in.nextInt();
amount = choice;
} while (amount != 0);
System.out.println("Good luck!");
}
}
除此之外,您还可以添加一个断点来逐行跳过您的程序,这可以让您快速找到错误。
这个程序我修了好久,还是不知道哪里出了问题。 我的问题是,如果我想获得多于 1 张票,它会给我比预期更多的数组列表。我找不到一个模式,好像我输入2,我得到3,如果我输入3,我得到6,如果我输入4,我得到10.
输入:我想要多少张票的金额。
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayListLottery {
public static void main(String[] args) {
int range = 49, amount = -1, number = 0, choice = -1;
// ArrayList<Integer> tickets = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> games = new ArrayList<ArrayList<Integer>>();
do {
System.out.println("Enter amount of lottery tickets you want");
Scanner in = new Scanner(System.in);
if (amount < 0) {
amount = in.nextInt();
}
while (amount != 0) {
System.out.println("Entered while block");
for (int i = 0; i < amount; i++) {
// Create an arraylist for how
// many tickets i want
ArrayList<Integer> tickets = new ArrayList<Integer>();
games.add(tickets);
for (int k = 0; k < 6; k++) { // Limit the size of a ticket to
// 6
if (tickets.size() < 6) {
number = (int) (range * Math.random()) + 1;
tickets.add(number);
Collections.sort(tickets);
} else {
break;
}
}// limit for loop to 6 end
}// arraylist creator end
amount--;
System.out.println("Amount is " + amount);
}//while loop end
for (List<Integer> i : games) { //print out each ticket
for (Integer n : i) {
System.out.print(n + " ");
}
System.out.println();
} //print out for-loop end
games.clear();
System.out.println("Type 0 to exit, otherwise pick any other number ");
choice = in.nextInt();
amount = choice;
} while (amount != 0);
System.out.println("Good luck!");
}
}
Fibonacci number of amount
将是您的游戏列表大小。
这是因为您已经为 amount
.
用 if
语句替换你的 while
循环。
你可以放
amount--;
在生成arraylist的for循环中。
像这样:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayListLottery {
public static void main(String[] args) {
int range = 49, amount = -1, number = 0, choice = -1;
// ArrayList<Integer> tickets = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> games = new ArrayList<ArrayList<Integer>>();
do {
System.out.println("Enter amount of lottery tickets you want");
Scanner in = new Scanner(System.in);
if (amount < 0) {
amount = in.nextInt();
}
while (amount != 0) {
System.out.println("Entered while block");
for (int i = 0; i < amount; i++) {
// Create an arraylist for how
// many tickets i want
ArrayList<Integer> tickets = new ArrayList<Integer>();
games.add(tickets);
for (int k = 0; k < 6; k++) { // Limit the size of a ticket to
// 6
if (tickets.size() < 6) {
number = (int) (range * Math.random()) + 1;
tickets.add(number);
Collections.sort(tickets);
} else {
break;
}
}// limit for loop to 6 end
amount--;
}// arraylist creator end
System.out.println("Amount is " + amount);
}//while loop end
for (List<Integer> i : games) { //print out each ticket
for (Integer n : i) {
System.out.print(n + " ");
}
System.out.println();
} //print out for-loop end
games.clear();
System.out.println("Type 0 to exit, otherwise pick any other number ");
choice = in.nextInt();
amount = choice;
} while (amount != 0);
System.out.println("Good luck!");
}
}
除此之外,您还可以添加一个断点来逐行跳过您的程序,这可以让您快速找到错误。