Quick Java Help Needed (updated)
Can someone please fix this code for me to display the right amount of tickets as defined by the user? I can always get it to display 1:
Can someone please fix this code for me to display the right amount of tickets as defined by the user? I can always get it to display 1:
Code:
import java.util.Scanner; // Scanner class
import java.util.Arrays; // Arrays class
public class Super7
{
public static void main(String[] args)
{
// Create a Scanner object for user input
Scanner input = new Scanner(System.in);
int tickets;
int ticketBoard = 21;
// display a title
System.out.println(); // blank line
System.out.println("Get Your Super 7 Lottery Numbers!");
System.out.println(); // blank line
// ask the user for the amount of tickets
System.out.print("How many tickets would you like on your board? ");
tickets = input.nextInt();
System.out.println();
System.out.print("Thank you for your purchase! Here are your "
+ tickets + " ticket(s): \n");
System.out.println();
// Create an array of 7 integers
int[] nums = new int[7];
// Populate the array with random numbers (1-47)
while(ticketBoard > 0 && tickets > 0)
{
for(int i = 0; i < nums.length; ++i)
{
// Assign a random number
nums[i] = (int)(47*Math.random()+1);
// Compare this number with all previous numbers
for(int j = 0; j < i; ++j)
{
if(nums[i] == nums[j])
{
--i;
}
}
}
//while(tickets > 0)
// Display the elements of the array
for(int i = 0; i < nums.length; ++i)
{
// Sort the array
Arrays.sort(nums);
// Print the tickets
System.out.print(nums[i] + "\t");
--ticketBoard;
}
System.out.println(); // print a blank line
}
}
}
Last edited:








