Reputation: 143
I have been tinkering with this program for a while, but I still do not know what is wrong. My problem is that if I want to get more than 1 ticket it gives me more array lists than expected. I cannot find a pattern as if I enter 2, I get 3, and if I enter 3, I get 6, and if I enter 4, I get back 10.
Input: Amount for how many tickets I want.
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!");
}
}
Upvotes: 0
Views: 476
Reputation: 11075
You can put
amount--;
in the for loop which generate the arraylist.
Like this:
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!");
}
}
Besides that, you can add a breakpoint to step over your program line by line which may make you find the bug quickly.
Upvotes: 1
Reputation: 4692
Fibonacci number of amount
will be your size of games list.
It is happening because you have applied while as well as for loop for amount
.
Replace your while
loop with if
statement.
Upvotes: 2