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(); } }