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 6 of 6

Thread: Help needed with constructing a while loop please!

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help needed with constructing a while loop please!

    My task is to: Prompt the user for and read in the number of guesses the user wants at the random number.
    2. Generate (but not immediately display) the random number between 1 and 100.
    3. As long as the user has not yet guessed the random number and the number of guesses
    allowed has not been exceded:
    • Prompt the user to enter their next guess at the generated random number.
    • If the guess is smaller than the actual random number, print out a message indicating
    that the user’s guess is too small.
    • If the guess is larger than the actual random number, print out a message indicating
    that the user’s guess is too large.
    4. If the user succeeded in guessing the random number, then print out a congratulatory
    message and tell them how many guesses they took.
    5. If the user did not succeed in guessing the random number, the number should be displayed.

    import java.util.*;
     
    public class SimpleGame {
     
    public static void main(String args[])
        {
        int numGuess;
        int userGuess;
     
        // Prompt User for Number of guesses as well as What guess the user intends to input
        Scanner kbd = new Scanner(System.in);
        System.out.print("How many guesses do you want?");
        numGuess = kbd.nextInt();
     
        System.out.println("What is your guess?");
        userGuess = kbd.nextInt();
     
     
        while (numGuess == 1)
     
        {
               if (userGuess == 63)
                   System.out.print("Congratulations, you guessed the number right!");
               else if (userGuess >= 63)
                   System.out.println("Sorry, your guess is too high");
               else if (userGuess <= 63)
                   System.out.println("Sorry, your guess is too low");
                   System.out.println("The number was 63");
               break;
     
        }        
     
    while (numGuess == 2)
     
        {
               if (userGuess == 36)
                   System.out.print("Congratulations, you guessed the number right!");
               else if (userGuess >= 36)
                   System.out.println("Sorry, your guess is too high");
               else if (userGuess <= 36)
                   System.out.println("Sorry, your guess is too low");
                   System.out.println("The number was 36");
               userGuess += 2;
               numGuess += 2;
               break;
     
        }

    So when i run this, it prompts me once yeah but i am a bit confused as to how i may get it to prompt me 2 or 3 or 4...etc times in a row, so if anyone could assist me with this, that would be very helpful.

    thanks


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Help needed with constructing a while loop please!

    A way to accomplish that is with a do...while loop. You can first ask the user how many guesses he want and then execute the do block as many times as the guesses.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with constructing a while loop please!

    thank you, i have done that

    public class SimpleGame {
     
    public static void main(String args[])
    	{
    	int numGuess;
    	int userGuess = 0;
    	Scanner kbd = new Scanner(System.in);
     
     
     
     
     
     
    	do 
    	{
    		//Prompt User for Number of guesses as well as What guess the user intends to input
    		System.out.print("How many guesses do you want?");
    		numGuess = kbd.nextInt();
     
    		System.out.println("What is your guess?");
     
    		if (numGuess == 1)
    		{
    		userGuess = kbd.nextInt();
    		}
     
    		if (numGuess == 2)
    		{	
    		userGuess = kbd.nextInt();
    		userGuess = kbd.nextInt();
     
    		}
    		if (numGuess == 3) 
    		{
    		userGuess = kbd.nextInt();
    		userGuess = kbd.nextInt();
    		userGuess = kbd.nextInt();
    		}
    		if (numGuess == 4)
    		{	
    		userGuess = kbd.nextInt();
    		userGuess = kbd.nextInt();
    		userGuess = kbd.nextInt();
    		userGuess = kbd.nextInt();
    		}
     
    		if (numGuess ==5)
    		{
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
     
    		}
     
     
    		if (numGuess ==6)
    		{
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
     
     
    		}
     
    		if (numGuess ==7)
    		{
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    		}
     
    		if (numGuess ==8)
    		{
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
    			userGuess = kbd.nextInt();
     
    		}
     
     
    }
     
     
     
    	while (numGuess == userGuess);
     
     
    	       if (userGuess == 63)
    	    	   System.out.print("Congratulations, you guessed the number right!");
    	       else if (userGuess >= 63)
    	    	   System.out.println("Sorry, your guess is too high");
    	       else if (userGuess <= 63)
    	    	   System.out.println("Sorry, your guess is too low");
    	           System.out.println("The number was 63");
     
     
     
     
    	}
    }

    now, i need to find a way to implement a infinite amount of guesses, if you could assist me with this, that would be great.

    thanks

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with constructing a while loop please!

    andreas90
    I have tried to simplify the code as my previous code was a bit much
    this is how it looks:
     
     
     
    import java.util.*;
    public class SimpleGme2 {
     
     
    	public static void main(String args[])
    	{
    	int counter = 0;
    	int numGuess;
    	int userGuess = 0;
    	Scanner kbd = new Scanner(System.in);
    	Random n = new Random ();
    	int number = 0;
     
     
    	//Prompt User for Number of guesses as well as What guess the user intends to input
    	    System.out.print("How many guesses do you want?");
     
     
     
    	while (counter < 10)
    	{
    		number = n.nextInt();
    		numGuess = kbd.nextInt();
     
    	if (number == 63 )
    		System.out.print("Congratulations, you guessed the number right!");
    	    break;	
    		}
     
    	if (number >= 63)
    		System.out.println("Sorry, your guess is too high");
     
    	else if (number <= 63)
    		System.out.println("Sorry, your guess is too low");
     
     
     
     
     
     
     
    	}
    }

    thanks

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Help needed with constructing a while loop please!

    Okay there is a problem with your pseudo-code. This is what your program is doing:

    Do infinite number of times   (note that the counter is never incremented so it will never be greater than 10)
       Generate a random number  (note that this is never used)
       Ask the user for a number
       If number == 63  print "Congratulations"
       If number < 63 print "Higher"
       If number > 63 print "Lower"
    End do

    Here is the proper pseudo-code for a higher/lower game
    Do
       Ask the user for the maximumNumberOfGuesses
       Generate a random number (number)
       Set guessCount == 0
       Do
          Ask the user for an input (userInput)
          Increment the guess count (count)
          If count > maximumNumberOfGuesses
              print "You have run out of guesses. You Loose."
              print "The correct answer was: " + number
              exit Do
          If number < userInput print "Higher"
          If number > userInput print "Lower"
          If number == userInput 
              print "Congratulations. You Win."
              Ask if the user wants to play again
              if true
                 go to start of the program
              else
                 exit program
           End do
    End do

    I cannot just give you the code for this as it would rob you of the learning experience. In fact providing you with the pseudo-code like this is probably the same thing because this is an exercise in structuring a program.

    Do me a favor and take a look at how I analyzed the question and wrote it out as a step of instructions that a human could follow step by step. This is called pseudo-code and it is essential for structuring your programs correctly. Your code is pretty good for a new programmer but I wanted to help you out here because the code reminded me of my own when I first started; syntactically correct but schematically wrong. So PLEASE do not just look at this pseudo-code and start writing code. Go over the question again and write your own pseudo-code. Compare it to mine if you need. Then start writing code. My golden rule is; every 10 minutes of planning will save you 1hr of programming.

  6. #6
    Junior Member
    Join Date
    Feb 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help needed with constructing a while loop please!

    Christopher Lowe, thank you for the help, i will put your advise to good use.

    Thanks once again.

Similar Threads

  1. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  2. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  3. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  4. Constructing Strings
    By Farmer in forum Loops & Control Statements
    Replies: 5
    Last Post: September 3rd, 2011, 10:24 AM
  5. Java Program Loop Possibly needed
    By mikec2500 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 25th, 2011, 01:35 AM