Help with Java again...

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.

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:
 
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");
 			}
 
 		 }
  }
 
it just tells the program how to read anything typed
 
i digress.. but is X-istence's avatar "legal"? lol.. i wan one too :p :lol:
 
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
 
Yeah, ya kinda lost me on that one :blink: :)
 
nevermind... I thought you guys could handle it but I thought wrong.
:D

I got it anyway
 
when do we get paid for helping you do your homework :confused:
 
not homework... it was practice and when and if you could help you would have my undying appreciation.
 
You should jump to my site khayman and pk, there are some people that need java help.
 
actually I need the practice but I am fired for the next few days ... Java has kicked my ass...
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back