我已经修改这个程序有一段时间了,但我仍然不知道出了什么问题。 我的问题是,如果我想获得超过 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!");
}
}
最佳答案
斐波那契数量
将是您的游戏列表的大小。
发生这种情况是因为您对 amount
应用了 while 和 for 循环。
将 while
循环替换为 if
语句。
关于java - ArrayList彩票游戏-多余的数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28271615/