Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am trying something.......

 
James Swanson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I am just learning java, and i have a project that is supposed to simulate a lotto game ( like megabucks/powerball or something) right now i am using to different sets of four random number generators and i want to use a for loop to make it play in circles until the plater wins.

rand1 = numberGenerator.nextInt(9);
rand2 = numberGenerator.nextInt(9);
rand3 = numberGenerator.nextInt(9);
rand4 = numberGenerator.nextInt(9);

yours1 = numberGenerator.nextInt(9);
yours2 = numberGenerator.nextInt(9);
yours3 = numberGenerator.nextInt(9);
yours4 = numberGenerator.nextInt(9);

are my generators.....the rand1,2,3,4 are the winning numbers and the yours are the user or players numbers,i don't want the game to end until they match (like the lottorey) if anyone could help it would be great...
if not a for loop any other sugestions are also welcome.

thanks
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post a little more of your code, so we can see how you are comparing the generated numbers. Are you selecting a set of numbers for a player and then looping the 'Draw' numbers until they match or are you regenerating player and drawn numbers on each iteration?
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It almost seems easier (instead of comparing the numbers at each iteration) to add the numbers to two separate arrays, and then use a compareTo method to compare each number that is in the array. Or you could just use a bunch of switches or if statements for each interation like Nigel was saying. As for the loop until ending, create a boolean isMatched method and use a while loop for the entire run. But again, we need your code to see how you are doing it now. We are not going to write the code for you my friend
[ October 20, 2004: Message edited by: Gabriel White ]
 
James Swanson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for now I plan to regenerate player and draw numbers each time so its like a new game every time. However once I get that to work I am going to try adding switches.


my code looks like this so far

import java.util.*;


public class newprogram{


public static void main (String[] args){

boolean done = false;
boolean playagain = true;
int rand1, rand2, rand3, rand4, yours1, yours2, yours3, yours4;
Random numberGenerator = new Random();
Scanner scan = new Scanner(System.in);

final String playmessage = "Would l you like to play the lotto? (y / n)";
final String youlost = "Sorry you did not win, buy another ticket?";
final String youwin = "you won the lotto, go quit your job!!!";


System.out.println(playmessage);
if (scan.next().equals("n"))
playagain = false;
else
done = false;


rand1 = numberGenerator.nextInt(9);
rand2 = numberGenerator.nextInt(9);
rand3 = numberGenerator.nextInt(9);
rand4 = numberGenerator.nextInt(9);


yours1 = numberGenerator.nextInt(9);
yours2 = numberGenerator.nextInt(9);
yours3 = numberGenerator.nextInt(9);
yours4 = numberGenerator.nextInt(9);


System.out.println ("The winning Lotto numbers are: " + rand1 +rand2 + rand3 + rand4);
System.out.println ("Your numbers cam up: " + yours1 + yours2 + yours3 + yours4);


while (playagain){
if (rand1 == yours1)
done = true;
else if (rand2 == yours2)
done = true;
else if (rand3 == yours3)
done = true;
else if (rand4 == yours4)
done = true;
else
done = false;
}
while (done = false)
System.out.println(youlost);







}
}

what I plan on doing is just make both generators to draw until the numbers match.
and sorry for the lack of info, however I do plan on doing this myself, i am just so new that i think I need to be pointed in a new direction.
also I have never done a for loop and don't understand it very well thats why I am asking.....not to get someone else to do work for me, sorry if I came across that way.

thank you for your reply
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to start by just restating the problem in something not quite code ...

Then I'd try to make Java code that looks as much like that as possible. The idea is for a reader to instantly comprehend the meaning of the program without having to read the whole thing.

That describes the intent of the program pretty well. But it should lead to a ton of questions ... what kind of variables would you use for win, quit, myNumbers, yourNumbers? What would you do in generateNumbers(), compareNumbers(), askUser()?

See if you can flesh out some of those answers for yourself. Make each of those methods describe what it's doing as clearly as the main one. Lemme know if that kind of thinking helps, show us where you go next
 
James Swanson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this is what i have so far.....

import java.util.*;


public class thelotto{


public static void main (String[] args){

boolean done = false;
boolean win = true;
boolean playagain = true;
int rand1, rand2, rand3, rand4, yours1, yours2, yours3, yours4;
Random numberGenerator = new Random();
Scanner scan = new Scanner(System.in);

String playmessage = "Would l you like to play the lotto? (y / n)";
String youlost = "Sorry you did not win, buy another ticket?";
String youwin = "you won the lotto, go quit your job!!!";





System.out.print(playmessage);
if (scan.next().equals("n"))
playagain = false;
else
done = false;


rand1 = numberGenerator.nextInt(9);
rand2 = numberGenerator.nextInt(9);
rand3 = numberGenerator.nextInt(9);
rand4 = numberGenerator.nextInt(9);


yours1 = numberGenerator.nextInt(9);
yours2 = numberGenerator.nextInt(9);
yours3 = numberGenerator.nextInt(9);
yours4 = numberGenerator.nextInt(9);




System.out.println ("The winning Lotto numbers are: " + rand1 + rand2 + rand3 + rand4);
System.out.println ("Your numbers cam up: " + yours1 + yours2 + yours3 + yours4);



if (rand1 != yours1)
done = false;
else if (rand2 != yours2)
done = false;
else if (rand3 != yours3)
done = false;
else if (rand4 != yours4)
done = false;
else
win = true;
}
}

can anyone tell me what i have done wrong with my loop?
i would like the numbers to regenerate until a win
however all i get is the first round displayed with no loop....
thanks for all that have replied so far
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic