P
PseudoKiller
Guest
I am trying to make a Java program to play a Hi Lo game using the 'while' loop.
The program has to guess a number between 1 to 100.
Then repeatedly prompt the user for a number while telling the user whether the number they guessed was higher or lower than the number generated.
It also needs to keep track of the number of guesses and when the user correctly guess or quits it will report the correct number and how many times the user guessed...
So far I can get it ask the user and tell whether it's higher or lower. Then it just stops.
now its all messed up and I dont know where to start... HELP
The program has to guess a number between 1 to 100.
Then repeatedly prompt the user for a number while telling the user whether the number they guessed was higher or lower than the number generated.
It also needs to keep track of the number of guesses and when the user correctly guess or quits it will report the correct number and how many times the user guessed...
So far I can get it ask the user and tell whether it's higher or lower. Then it just stops.
Code:
[font=Courier New][color=#0000ff]
3
4 [/color][/font][font=Courier New][color=#941edf]import[/color][/font][font=Courier New] cs1.Keyboard;
[/font][font=Courier New][color=#0000ff]5 [/color][/font][font=Courier New][color=#941edf]import[/color][/font][font=Courier New] java.util.Random;
[/font][font=Courier New][color=#0000ff]6
7 [/color][/font][font=Courier New][color=#941edf]public[/color][/font][font=Courier New][color=#941edf]class[/color][/font][font=Courier New] HiLo
[/font][font=Courier New][color=#0000ff]8 [/color][/font][font=Courier New]{
[/font][font=Courier New][color=#0000ff]9 [/color][/font][font=Courier New][color=#941edf]public[/color][/font][font=Courier New][color=#941edf]static[/color][/font][font=Courier New][color=#941edf]void[/color][/font][font=Courier New] main (String[] args)
[/font][font=Courier New][color=#0000ff]10 [/color][/font][font=Courier New]{
[/font][font=Courier New][color=#0000ff]11 [/color][/font][font=Courier New][color=#941edf]final[/color][/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] MAX = 100;
[/font][font=Courier New][color=#0000ff]12 [/color][/font][font=Courier New][color=#941edf]int[/color][/font][font=Courier New] answer, guess;
[/font][font=Courier New][color=#0000ff]13 [/color][/font][font=Courier New][color=#0000ff]14 [/color][/font][font=Courier New]Random generator = [/font][font=Courier New][color=#941edf]new[/color][/font][font=Courier New] Random();
[/font][font=Courier New][color=#0000ff]15 [/color][/font][font=Courier New]answer = generator.nextInt(MAX) + 1;
[/font][font=Courier New][color=#0000ff]16 [/color][/font][font=Courier New][color=#0000ff]17 [/color][/font][font=Courier New]System.out.print ([/font][font=Courier New][color=#00cb00]"I'm thinking of a number between 1 and "
[/color][/font][font=Courier New][color=#0000ff]18 [/color][/font][font=Courier New]+ MAX + [/font][font=Courier New][color=#00cb00]". Guess what it is: "[/color][/font][font=Courier New]);
[/font][font=Courier New][color=#0000ff]19
20 [/color][/font][font=Courier New]guess = Keyboard.readInt();
[/font][font=Courier New][color=#0000ff]21 [/color][/font][font=Courier New][color=#0000ff]22 [/color][/font][font=Courier New][color=#941edf]while[/color][/font][font=Courier New] (guess != 0);
[/font][font=Courier New][color=#0000ff]23
24 [/color][/font][font=Courier New][color=#941edf]if[/color][/font][font=Courier New] (guess < answer)
[/font][font=Courier New][color=#0000ff]25 [/color][/font][font=Courier New]System.out.println ([/font][font=Courier New][color=#00cb00]"Guess a higher number"[/color][/font][font=Courier New]);
[/font][font=Courier New][color=#0000ff]26 [/color][/font][font=Courier New]guess = Keyboard.readInt();
[/font][font=Courier New][color=#0000ff]27
28 [/color][/font][font=Courier New][color=#941edf]if[/color][/font][font=Courier New] (guess == answer)
[/font][font=Courier New][color=#0000ff]29 [/color][/font][font=Courier New]System.out.println ([/font][font=Courier New][color=#00cb00]"You guessed correctly"[/color][/font][font=Courier New]);
[/font][font=Courier New][color=#0000ff]30
31 [/color][/font][font=Courier New][color=#941edf]else
[/color][/font][font=Courier New][color=#0000ff]32 [/color][/font][font=Courier New]System.out.println ([/font][font=Courier New][color=#00cb00]"Guess a lower humber"[/color][/font][font=Courier New]);
[/font][font=Courier New][color=#0000ff]33
34 [/color][/font][font=Courier New]}
[/font][font=Courier New][color=#0000ff]35 [/color][/font][font=Courier New]}
[/font]
now its all messed up and I dont know where to start... HELP