So I created a guessing game where the user has three guesses and has to guess the random generated number. I think I have it all under control, but there is a bug that I can't find and fix it. When the user guess the correct number, it should say "You won!", but my program doesn't do that. Need help.
import java.util.Scanner;
public class guess {
public static void main(String[] args) {
Scanner firstG = new Scanner(System.in);
Scanner secondG = new Scanner(System.in);
Scanner thirdG = new Scanner(System.in);
int randomN = (int)(Math.random() * 10) +1;
System.out.println("I'm thinking of an integer in the range 1 to 10. You have three guess.");
System.out.println();
System.out.print("Enter your first guess: ");
int guess1 = firstG.nextInt();
if(guess1 > randomN) {
System.out.println("Too high!");
if(guess1 > randomN) {
System.out.println();
System.out.print("Enter your second guess: ");
int guess2 = secondG.nextInt();
if (guess2 > randomN) {
System.out.println("Too high!");
if (guess2 > randomN) {
System.out.println();
System.out.print("Enter your third guess: ");
int guess3 = thirdG.nextInt();
if (guess3 > randomN) {
System.out.println("Too high!");
System.out.println();
System.out.println("You lose. The was " + randomN);
if(guess1 == randomN) {
System.out.println("You win!");
if(guess2 == randomN) {
System.out.println("You win!");
if(guess3 == randomN) {
System.out.println("You win!");
}
}
}
}
}
}
}
}
if(guess1 < randomN) {
System.out.println("Toolow!");
if(guess1 < randomN) {
System.out.println();
System.out.print("Enter your second guess: ");
int guess2 = secondG.nextInt();
if(guess2 < randomN) {
System.out.println("Too low!");
if (guess2 < randomN) {
System.out.println();
System.out.print("Enter your third guess: ");
int guess3 = thirdG.nextInt();
if(guess3 < randomN) {
System.out.println("Too low");
System.out.println();
System.out.println("You lose. The number was " + randomN);
if(guess1 == randomN) {
System.out.println("You win!");
if(guess2 == randomN) {
System.out.println("You win!");
if(guess3 == randomN) {
}
}
}
}
}
}
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire