NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Quick Java Help Needed

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

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 11-03-2007, 02:32 PM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: May 2006
Posts: 311
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 09:41 PM.
Hayes is offline  
Old 11-10-2007, 09:40 PM   #2 (permalink)
DNOA Member
Join Date: May 2004
Posts: 5,040
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


Liquid Web Smart Servers  
All times are GMT -7. The time now is 12:10 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger