[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 11-03-2007, 01:32 PM   #1 (permalink)
NamePros Regular
 
Join Date: May 2006
Posts: 305
657.20 NP$ (Donate)

Hayes has a spectacular aura aboutHayes has a spectacular aura about


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 08:41 PM.
Hayes is offline  
Old 11-10-2007, 08:40 PM   #2 (permalink)
DNOA Member
 
mholt's Avatar
 
Join Date: May 2004
Location: Utah
Posts: 5,041
18.01 NP$ (Donate)

mholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant futuremholt has a brilliant future

Autism Marrow Donor Program 9/11/01 :: Never Forget Multiple Sclerosis Adoption Alzheimer's Lou Gehrig's Disease (ALS)
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?
mholt is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 11:43 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85