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

Thread: If statements-printing messages

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default If statements-printing messages

    Hello guys,
    I have a small problem with my code that I can't figure out how to make it work the way it is supposed to. The code is supposed to be a game where a user has to guess numbers between 1-1000. The program counts how many times the user tried to guess the number and it displays a certain message if the guess number is less than 10, more than 10 or 10. I was able to write the code using loops. However, the messages will not always get printed on to the screen. The code seems to work fine except for the last part where the messages, "Either you know the secret or you got lucky", "You should be able to do better", "Aha! you know the secret!" are not always displayed like they are supposed to. Thanks for the help in advance.

     
    import java.util.Scanner;
     
    public class Guess1 {
          public static void main(String[] args) {
               int secretNumber;
               secretNumber = (int) (Math.random() * 999 + 1);           
                Scanner input = new Scanner(System.in);
               int guess;
               int replay;
               int test;       
               test=1;      
               replay=1;    
               int count=0;
     
                while (replay == test)   /// start loop
                {
                do {
                	count++; /// start count
                      System.out.print("Enter a guess (1-1000): ");
     
                      guess = input.nextInt();
     
                      if (guess == secretNumber)
                            System.out.println(" Congratulations. You guessed the number!");
                      else if (guess < secretNumber)
                            System.out
                                       .println("Too low. Try again.");
                      else if (guess > secretNumber)
                            System.out.println("Too high. Try again.");
                } while (guess != secretNumber);
     
                System.out.printf("It took you %d trial/s to get the right number.", count); 
                	System.out.println("");
     
                	{   
                if (count <=10)
                {System.out.println("Either you know the secret or you got lucky!");
                 if  (count>10)
                	System.out.println("You should be able  to do better");
               if (count==10)
                	System.out.println("Aha! you know the secret!");
                }
                	}
     
                System.out.println("Do you want to play again? 1 for Yes. 0 for No");
                replay = input.nextInt();
    }
    }


  2. #2
    Junior Member
    Join Date
    Jun 2014
    Posts
    22
    My Mood
    Starving
    Thanks
    1
    Thanked 8 Times in 6 Posts

    Default Re: If statements-printing messages

    Okay few things I noticed,

    pay close attention to your brackets {}

    you will never reach this statement

     if (count>10)
    because it is inside of
    if(count <=10)
    I know this because there is a bracket after that and the "if (count>10)" is inside that bracket.
    The only way it will get inside that is if count<=10, so logically you won't ever get that message.

    I am referring to the bracket at the beginning of this line:
    {System.out.println("Either you know the secret or you got lucky!");
    That bracket at the beginning comes after the first if statement. Anything that follows that ( until it is closed with } ) only executes when that if statement resolves to true.

    Also, anytime count ==10, two messages are going to display.
    because:

     
                if (count <=10)
                {System.out.println("Either you know the secret or you got lucky!");
     
               if (count==10)
                	System.out.println("Aha! you know the secret!");

    both of these if statements resolve to true when count==10, the first will also work for less than ten because it is <= (less than OR equal to)
    I assume you just want that to be < (less than)


    Tell me, Do you think the brackets { } in this code are necessary?
                	{   
                if (count <=10)
                {System.out.println("Either you know the secret or you got lucky!");
                 if  (count>10)
                	System.out.println("You should be able  to do better");
               if (count==10)
                	System.out.println("Aha! you know the secret!");
                }
                	}


    Sorry for being lengthy, but I didn't want to give you the answer, I'd rather help you understand the why behind it

  3. The Following 3 Users Say Thank You to koder632417 For This Useful Post:

    Ada Lovelace (June 12th, 2014), GregBrannon (June 12th, 2014), toddy (June 12th, 2014)

  4. #3
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    385
    My Mood
    Angelic
    Thanks
    27
    Thanked 55 Times in 49 Posts

    Default Re: If statements-printing messages

    Just to add something extra to what koder632417 said:

    As a general of thumb to any C-like language, if you know that the body of an if statement
    will contain more than a single line, then it's a good idea to encase the code so it remains in the
    correct scope. Think of this when your addressing the problems specified above, just to gain a bit
    of extra understanding of the importance of variable scoping.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. The Following 2 Users Say Thank You to Ada Lovelace For This Useful Post:

    koder632417 (June 12th, 2014), toddy (June 12th, 2014)

  6. #4
    Junior Member
    Join Date
    May 2014
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: If statements-printing messages

    Thank you very much for the comment. I removed the brackets and it works perfectly now.

Similar Threads

  1. Questions about if statements and if-else statements
    By chakana101 in forum Java Theory & Questions
    Replies: 5
    Last Post: March 23rd, 2014, 05:37 PM
  2. Replies: 3
    Last Post: November 30th, 2013, 04:52 PM
  3. Javamail messages
    By arczi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2013, 02:18 AM
  4. Replies: 1
    Last Post: September 28th, 2011, 07:29 AM
  5. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM

Tags for this Thread