| | |||||
| ||||||||
| Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics. |
![]() |
| | LinkBack | Thread Tools |
| | THREAD STARTER #1 (permalink) |
| NamePros Regular Join Date: May 2006
Posts: 311
![]() ![]() | 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: 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 by Hayes; 11-03-2007 at 09:41 PM.
|
| |
| | #2 (permalink) |
| DNOA Member Join Date: May 2004
Posts: 5,040
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() | That... should work... I'm assuming it prints this? "Thank you for your purchase! Here are your 1 ticket(s)" 1. There's no compile-time errors, I assume. That's good. 2. You declare the variable as an int. That's all good. 3. You're using the Scanner class correctly. Good... 4. You use the nextInt() method correctly, good. And you assign its return that int you created. Good. 5. You print out that int variable (tickets) later on without modifying it. All good. I am stumped as to why it prints 1 every time... are you sure you aren't inputting 1 each time? |
| |