Help with Java again...

Discussion in 'Web Design & Coding' started by PseudoKiller, Feb 23, 2004.

  1. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    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:
     
  2. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    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");
     			}
     
     		 }
      }
     
     
  3. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    YOU ARE A GOD .... I owe you BIG TIME !!!!
     
  4. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    What is this cs1.keyboard stuff? I dont have that :(
     
  5. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    it just tells the program how to read anything typed
     
  6. falcone

    falcone WinFS Advocate :D

    Messages:
    50
    Location:
    Singapore
    i digress.. but is X-istence's avatar "legal"? lol.. i wan one too :p :lol:
     
  7. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Want one what?
     
  8. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    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
     
  9. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
  10. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    Yeah, ya kinda lost me on that one :blink: :)
     
  11. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    nevermind... I thought you guys could handle it but I thought wrong.
    :D

    I got it anyway
     
  12. Sazar

    Sazar F@H - Is it in you? Staff Member Political User Folding Team

    Messages:
    14,905
    Location:
    Between Austin and Tampa
    when do we get paid for helping you do your homework :confused:
     
  13. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    not homework... it was practice and when and if you could help you would have my undying appreciation.
     
  14. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    You should jump to my site khayman and pk, there are some people that need java help.
     
  15. PseudoKiller

    PseudoKiller Zug Zug

    Messages:
    3,858
    Location:
    Ice Crown Citadel
    actually I need the practice but I am fired for the next few days ... Java has kicked my ass...