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

Thread: Problem with my code.. when i enter zero...

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Problem with my code.. when i enter zero...

    I will post both the project and code below... I have two questions. When the user enters O to quit why can I not get my response to display? When I enter 0 the program just quits but I want it to display "Thanks for playing. Good Bye" Also, should I be able to enter a number higher than 100 if it is only suppose to be 1- 100 inclusive? if I enter 101 or 102 it still will tell me if the number is too high. My code compiles and runs except for these two problems. Thank you for your help.


    Design and implement an application that plays a guessing game with numbers. The program should generate a random number between 1 and 100 (inclusive), then repeatedly prompt the user to guess the number. On each guess, display a message to the user indicating their guess is correct or the guess is too high or too low. Continue accepting guesses until the user guesses correctly or chooses to quit. Accept a sentinel value (0 is often used) to allow the user to signal they want to quit. Keep a count the number of guesses and report the count when the user guesses correctly. At the end of each game (by quitting or a correct guess), prompt the user to indicate whether they want to play again. Continue playing games until the user chooses to stop.
    import java.util.Scanner;
    import java.util.Random;
     
     
    public class GuessingGame
    {
       public static void main (String[] args)
       {
          final int MAX = (100);
          int guess, number;
     
          java.util.Scanner scan = new java.util.Scanner(System.in);
          System.out.print (" Can you guess the number I am thinking of?" +
          " Guess what it is between 1 and 100(or 0 to quit) ");
     
          //Random generator 1-100
          guess = scan.nextInt();
          Random generator = new Random();
          number = generator.nextInt(MAX)+1;
     
          //If statement 
     
          if (guess == number)
          {
             System.out.println (" Great guess!! You got it right!! ");
          }
          //if user enters 0
          else if (number == 0)
          {
             System.out.println (" Thanks for playing. Good bye. ");
          }
          // guess is not 0 or number
          while (guess != number && guess != 0)
          {
          //guess is higher than number
          if (guess > number && guess != 0)
          {
             System.out.println (" Your guess is too high! ");
             guess = scan.nextInt();
     
             }
             else 
             {
             // guess is lower thsn number
                if (guess < number && guess !=0)
                {
                   System.out.println (" Your guess is too low!");
                   guess = scan.nextInt();
                }
     
                //guess is equal to number
                else if ( guess == number)
                {   
                   System.out.println(" Great guess!! You got it right!! ");
                }
     
     
               //game ends
               else if (guess == 0)
                {
                   System.out.println (" Thanks for playing. Good bye. ");   
                }
               }
              }
     
                if (guess == number)
                {
                   System.out.println(" Great guess!! You got it right! ");
     
                }
     
     
        }
      }


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

    Default Re: Problem with my code.. when i enter zero...

    You should use a loop to validate the user's input before seeing if it matches the target.
    Put that loop inside of the main loop that plays the game.
    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:

    squirrelmind (June 20th, 2014)

Similar Threads

  1. Replies: 7
    Last Post: February 10th, 2014, 09:45 AM
  2. Replies: 1
    Last Post: February 27th, 2013, 02:32 AM
  3. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM
  4. Why does it enter this if ?
    By piulitza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2011, 11:42 AM