Help with Java again...

P

PseudoKiller

Guest
#1
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.

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 :confused:
 

Khayman

I'm sorry Hal...
Political User
Joined
Jan 6, 2002
Messages
5,518
#2
I did a quick pass at it, this works for me
Code:
 import cs1.Keyboard;
 import java.util.Random;
 
 public class HiLo
 {
 	public static void main (String[] args)
 		 {
 			 final int MAX = 100;
 			 int answer, guess=1;
 			 int guess_count=0;
 			 Random generator = new Random();
 			 answer = generator.nextInt(MAX) + 1;
 			 System.out.println ("I'm thinking of a number between 1 and "
 			 + MAX + ". Guess what it is: ");
 				
 			System.out.println ("Number ?");
 
 			 do
 			 {
 				 guess=Keyboard.readInt();
 				if (guess!=0)
 				{
 					if (guess < answer)
 						System.out.println ("Guess a higher number");
 					if (guess > answer)
 						 System.out.println ("Guess a lower humber");
 				}
 
 				guess_count++;
 			 }while ((guess != answer) && (guess != 0));
 
 			System.out.println ("The number was "+answer);
 			if (guess!=0)
 			{
 				System.out.println ("You guessed correctly");
 				System.out.println ("You took "+guess_count+" guesses");
 			}
 
 		 }
  }
 
P

PseudoKiller

Guest
#8
Java Problem Part 3:

I need to do have the out put do the following

11-12-13
21-22-23
31-32-33
41-42-43

I am allowed to use int r,c

I can use while, do, if / else and other loops but I am lost ... so confused and tired. Help me !!??

Thanks
 

Sazar

F@H - Is it in you?
Staff member
Political User
Joined
Apr 12, 2002
Messages
14,905
#12
when do we get paid for helping you do your homework :confused:
 
P

PseudoKiller

Guest
#13
not homework... it was practice and when and if you could help you would have my undying appreciation.
 
P

PseudoKiller

Guest
#15
actually I need the practice but I am fired for the next few days ... Java has kicked my ass...
 

Members online

Latest profile posts

Perris Calderon wrote on Electronic Punk's profile.
Ep, glad to see you come back and tidy up...did want to ask a one day favor, I want to enhance my resume , was hoping you could make me administrator for a day, if so, take me right off since I won't be here to do anything, and don't know the slightest about the board, but it would be nice putting "served administrator osnn", if can do, THANKS

Been running around Quora lately, luv it there https://tinyurl.com/ycpxl
Electronic Punk wrote on Perris Calderon's profile.
All good still mate?
Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
62,001
Messages
673,414
Members
89,024
Latest member
dianeperli