Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Random number guessing game with three tries (loops)

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Random number guessing game with three tries (loops)

    hi! this is my first time one a forum and learning code, so please bear with me.
    I was told to write a program which generates a random number between 0 to 5 *including 5*
    and give the user 3 chances to guess this number:
    If the user enters a wrong number within 0 to 5, the user has lost one opportunity.
    If the user enters a number out of range, the program should prompt the user to enter a number within the range
    If the user has 3 unsuccessful attempts, the program will print the number.
    i was also instructed to use
    Random randomNumber=new Random();
    int i= randomNumber.nextInt(6)

    i have attempted it and this is what i have so far.

    package assignment7;
     
    import java.util.Random;
    import java.util.Scanner;
     
    public class Exercise1 
    {
    	public static void main(String[] args) 
    	{
     
    		Random randomNumber=new Random();
    		Scanner scan=new Scanner(System.in);
    		int randomNumber,guessNumber;
     
    		randomNumber=randomGenerator.nextInt(4);
    		System.out.print("Guess the random number: " + "\n" + "you get three tries");
    		guessNumber=scan.nextInt();
     
    		for(int randomNumber;randomNumber<=4;randomNumber++)
    			randomNumber=randomNumber<=4;
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Random number guessing game with three tries (loops)

    What happens when you compile and execute the code?
    If there are error messages, copy and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    ATB (March 8th, 2014)

  4. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Random number guessing game with three tries (loops)

    what happens when i try to compile & run

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Duplicate local variable randomNumber
    randomGenerator cannot be resolved

    at assignment7.Exercise1.main(Exercise1.java:13)

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Random number guessing game with three tries (loops)

    Duplicate local variable randomNumber
    randomGenerator cannot be resolved
    Is the variable: randomNumber defined more than one time? Rename one of them to a different name.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Random number guessing game with three tries (loops)

    to be honest i'm not sure what im doing but i think im missing something
    here is the revised version
    Capture.jpg

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Random number guessing game with three tries (loops)

    PLease post the code here, not an image. Find/Search doesn't work with images.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    ATB (March 8th, 2014)

  9. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Random number guessing game with three tries (loops)

    sorry. im changing it again now i think this will be good

    package assignment7;
     
    import java.util.Scanner;
    import java.util.Random;
     
    public class Exercise1 
    {
     
    	static int lowNumber=0;
    	static int highNumber=5;
    	static int numOfGuessesAllowed=3;
     
    public static void main(String[] args) 
    	{
    		@SuppressWarnings("resource")
    		Scanner sc = new Scanner(System.in);
    		Random generator = new Random();
    		int target = generator.nextInt(highNumber) + lowNumber;
    		int numOfGuesses=0;
    		while(numOfGuesses < numOfGuessesAllowed) 
    	{
     
    	System.out.println("Guess a number:");
    	int guess = sc.nextInt();
    	boolean hasWon = checkNumber(guess, target);
    	numOfGuesses++;
    	if(hasWon) 
    	{
    		System.out.println("You win");
    		System.exit(0);
    	}
    	}
    		System.out.println("You lose");
    		System.exit(0);
    	}
     
    public static boolean checkNumber(int guess, int target) {
    	if(guess < target) 
    	{
    		System.out.println("Higher");
    	} else if (guess > target) {
    		System.out.println("Lower");
    	}
    	return guess==target;
    	}
     
    }


    Compile and Run:

    Guess a number:
    1
    Higher
    Guess a number:
    2
    Higher
    Guess a number:
    3
    Higher
    You lose
    Last edited by ATB; March 8th, 2014 at 07:14 PM.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,149
    Thanks
    65
    Thanked 2,722 Times in 2,672 Posts

    Default Re: Random number guessing game with three tries (loops)

    does it compile and execute now?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    ATB (March 8th, 2014)

  12. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    26
    My Mood
    Inspired
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Random number guessing game with three tries (loops)

    yes it does, and to print out the random number if the player loses would be the code below:
    if(hasWon) 
    	{
    		System.out.println("You win");
    		System.exit(0);
    	}
    	}
    		System.out.println("You lose" + "The random number was:" + target);
    		System.exit(0);
    	}

    and that compiles to :
    Guess a number:
    3
    Lower
    Guess a number:
    3
    Lower
    Guess a number:
    4
    Lower
    You loseThe random number was:2

    --- Update ---

    Thanks Norm!
    Last edited by ATB; March 8th, 2014 at 07:29 PM.

Similar Threads

  1. [SOLVED] Problem Script: Random Number Guessing Game
    By superjacko in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 7th, 2013, 09:57 PM
  2. Basic Number guessing game.
    By Ddng940 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2012, 08:01 PM
  3. Reverse Number guessing game
    By rarman555 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 23rd, 2011, 10:39 PM
  4. Number Guessing Game
    By JayK in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 16th, 2011, 09:46 PM
  5. Guessing Number Game Help
    By Shadow20 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2011, 12:41 PM

Tags for this Thread