ArrayList彩票游戏 - 多余的数组列表

时间:2015-02-02 05:30:36

标签: 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 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!");
   }
}

2 个答案:

答案 0 :(得分:2)

Fibonacci number of amount将是您的游戏列表大小。

这种情况正在发生,因为您已为amount应用了while和for循环。

while循环替换为if语句。

答案 1 :(得分:1)

你可以把

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!");
   }
}

除此之外,您可以添加一个断点来逐行切换程序,这可能会让您快速找到错误。