Results 1 to 6 of 6

Thread: Hi Lo Game

  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default Hi Lo Game

    Hi guys !!! how can i make a loop that will continually accept and check user input , if wrong prompts for another guess.
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class HiLo {
    public static void main(String[]args) {
    
    int c,b,d,e;
    Random generator = new Random();
    int a = generator.nextInt(100);
    Scanner scan = new Scanner(System.in);
    System.out.println("Guess any number between 0 -100 (inclusively) ");
    c =scan.nextInt();
    b =generator.nextInt(100);
    
    while  (c != b) {
    			System.out.println("WRONG GUESS AGAIN!!!!" +"(" + b+ ")" );
    			e = scan.nextInt();
    			Scanner s2 = new Scanner(System.in);
    		    d = generator.nextInt(100);
    			 }	
    	  }
    }// CODE COMPILES FINE BUT DOESNT WORK CORRECTLY.IT WONT GENERATE A DIFFERENT RANDOM NUMBER

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    In the while loop you are using variable 'e' as the user input. In the while loop declaration you are checking variable 'c' which is not changing in the loop. So change 'c' to 'e' and then initialize 'e'.
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Hey buddy thanks for the feedback i tried doing as you said and i got the exact same results heres what i got so far
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class HiLo {
    public static void main(String[]args) {
    
    int c,b;
    int d=0;
    int e = 0;
    Random generator = new Random();
    int a = generator.nextInt(100);
    Scanner scan = new Scanner(System.in);
    System.out.println("Guess any number between 0 -100 (inclusively) ");
    e =scan.nextInt();
    b =generator.nextInt(100);
    
    while  (e !=b ) {
    			System.out.println("WRONG GUESS AGAIN!!!!" +"(" + b+ ")" );
    			Random generator1 = new Random();
    			Scanner s2 = new Scanner(System.in);
    			e = s2.nextInt();
    		    d = generator1.nextInt(100);
    			 }	
    	  }
    }

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    Now you have a variable 'd' inside your loop instead of 'b'. Are you trying to make it so you can keep guessing without the program terminating?
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    Ah ha Thanks for the help..Got it running now.DDDD:eek::cool:

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    No problem, and gratz! :D
    Sincerely, Joshua Green
    Please REP if I help :)

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  2. game code for any game
    By deathnote202 in forum Java Gaming
    Replies: 4
    Last Post: 06-10-2010, 09:06 AM
  3. can someone help me with this game??
    By amalina in forum New To Java
    Replies: 1
    Last Post: 08-24-2009, 06:59 AM
  4. Game 21
    By aRTx in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 01:33 AM
  5. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •