# If statements-printing messages

• June 11th, 2014, 10:18 PM
toddy
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.

Code Java:

```  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(); } }```
• June 11th, 2014, 10:47 PM
koder632417
Re: If statements-printing messages
Okay few things I noticed,

pay close attention to your brackets {}

you will never reach this statement

Code :

` if (count>10)`
because it is inside of
Code :

`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:
Code :

`{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:

Code :

```  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?
Code :

``` { 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 :-ss
• June 12th, 2014, 03:31 AM