Results 1 to 3 of 3
  1. #1
    MR bruto's Avatar
    MR bruto is offline Senior Member
    Join Date
    May 2013
    Location
    The Netherlands
    Posts
    130
    Rep Power
    0

    Default Need a little help with this random numers generator.

    alright so i decided to improve my randoms numbers guessing game.

    i made this:
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class excercise {
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		Random rand = new Random();
    		int rand1 = rand.nextInt(10); 
    		int numguesses = 1;
    		System.out.println("Guess the right number 1-10: ");
    		while (rand1 != input.nextInt()){
    			System.out.println("Wrong! try again!");
    		    System.out.println("Guess the right number 1-10: ");
    		    numguesses++;
    		}
    		
    		System.out.println("Right! Turns used: "+numguesses);
    			
    			
    		
    	}
    }
    i really want to make it so that it tells you "Your number was not in the range of 1-10".
    i did it by this:
    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    public class excercise {
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		Random rand = new Random();
    		int rand1 = rand.nextInt(10); 
    		int numguesses = 1;
    		while (input.nextInt() > 10){
    			System.out.println ("The number you entered is out of the range 1-10! try again!");
    			System.out.println("Guess the right number 1-10: ");
    		}
    		
    		System.out.println("Guess the right number 1-10: ");
    		while (rand1 != input.nextInt()){
    			System.out.println("Wrong! try again!");
    		    System.out.println("Guess the right number 1-10: ");
    		    numguesses++;
    		}
    		
    		System.out.println("Right! Turns used: "+numguesses);
    			
    			
    		
    	}
    }
    i added in this part:
    Java Code:
    	while (input.nextInt() > 10){
    			System.out.println ("The number you entered is out of the range 1-10! try again!");
    			System.out.println("Guess the right number 1-10: ");
    		}
    but it doesnt seem to work for me.

    can anybody tell me how to fix it?
    because when i run it, and you enter your first number it basically does NOTHING. then if you enter your second number it just starts from here:
    Java Code:
    System.out.println("Guess the right number 1-10: ");
    		while (rand1 != input.nextInt()){
    			System.out.println("Wrong! try again!");
    		    System.out.println("Guess the right number 1-10: ");
    		    numguesses++;
    		}
    		
    		System.out.println("Right! Turns used: "+numguesses);
    			
    			
    		
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,727
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need a little help with this random numers generator.

    Try to say in words what you want; don't go into too much detail, that's for later. Try to use terms such as 'if' and 'while' and 'do'. A nice start would be:

    Java Code:
    do
       get a valid number
       vaidate the number
    while the number is not correct
    the phrase 'get a valid number' begs to be a separate method; methods are a fine way to unravel deeply nested control flow. Next try to go into more detail for the first phrase: what is a valid number? What is a number?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Need a little help with this random numers generator.

    From the API for nextInt(n):

    Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive), drawn from this random number generator's sequence.

    Therefore your code generates numbers from 0 - 9. Not what you want.

Similar Threads

  1. Random sentence generator
    By BrAD19924 in forum New To Java
    Replies: 6
    Last Post: 05-03-2011, 03:58 AM
  2. Help with Random Number Generator
    By celtics in forum New To Java
    Replies: 0
    Last Post: 03-07-2011, 09:18 PM
  3. Random Generator
    By Learning Java in forum New To Java
    Replies: 9
    Last Post: 05-31-2010, 09:15 PM
  4. Random number generator
    By Michailangelo in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 07:47 PM
  5. Random Generator
    By padutch2 in forum New To Java
    Replies: 1
    Last Post: 12-03-2007, 07:43 PM

Posting Permissions

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