Java ArrayList彩票游戏-多余数组列表
我一直在修补这个程序有一段时间了,但我仍然不知道出了什么问题。 我的问题是,如果我想获得超过1张票证,它会给我比预期更多的数组列表。我找不到一个模式,如果我输入2,我得到3,如果我输入3,我得到6,如果我输入4,我得到10 输入:我想要多少张票的金额Java ArrayList彩票游戏-多余数组列表,java,java.util.scanner,Java,Java.util.scanner,我一直在修补这个程序有一段时间了,但我仍然不知道出了什么问题。 我的问题是,如果我想获得超过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 Arr
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!");
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Scanner;
公共类阵列天线{
公共静态void main(字符串[]args){
整数范围=49,金额=1,数字=0,选项=1;
//ArrayList票证=新的ArrayList();
ArrayList games=新建ArrayList();
做{
System.out.println(“输入您想要的彩票数量”);
扫描仪输入=新扫描仪(系统输入);
如果(金额<0){
金额=in.nextInt();
}
而(金额!=0){
System.out.println(“在块中输入”);
对于(int i=0;i
斐波那契数量将是您游戏列表的大小
之所以会发生这种情况,是因为您已应用while以及for循环,以获得金额
将while
循环替换为if
语句。Fibonacci数量
将是游戏列表的大小
之所以会发生这种情况,是因为您已应用while以及for循环,以获得金额
将while
循环替换为if
语句。Fibonacci数量
将是游戏列表的大小
之所以会发生这种情况,是因为您已应用while以及for循环,以获得金额
将while
循环替换为if
语句。Fibonacci数量
将是游戏列表的大小
之所以会发生这种情况,是因为您已应用while以及for循环,以获得金额
将while
循环替换为if
语句。您可以将
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!");
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Scanner;
公共类阵列天线{
公共静态void main(字符串[]args){
整数范围=49,金额=1,数字=0,选项=1;
//ArrayList票证=新的ArrayList();
ArrayList games=新建ArrayList();
做{
System.out.println(“输入您想要的彩票数量”);
扫描仪输入=新扫描仪(系统输入);
如果(金额<0){
金额=in.nextInt();
}
而(金额!=0){
System.out.println(“在块中输入”);
对于(int i=0;i
除此之外,您还可以添加一个断点来逐行遍历程序,这可能会使您快速找到错误。您可以将
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!");
}
}
import java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.Scanner;
公共类阵列天线{
公共静态void main(字符串[]args){
整数范围=49,金额=1,数字=0,选项=1;
//ArrayList票证=新的ArrayList();
ArrayList games=新建ArrayList();
做{
System.out.println(“输入您想要的彩票数量”);
扫描仪输入=新扫描仪(系统输入);
如果(amo)