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();
}
}
Re: If statements-printing messages
Okay few things I noticed,
pay close attention to your brackets {}
you will never reach this statement
because it is inside of
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
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
Re: If statements-printing messages
Thank you very much for the comment. I removed the brackets and it works perfectly now.