I am making a number guessing game in where it generates a random number and the user has to guess it. I am asking the user to try again but if they say yes it gives the else statement and if they say no it works as planned.
I wanted to mention that the number guessing part does also not work. What I mean is that it is for some reason not letting the user ever guess the number right. It doesn't give me any error so I do not know what to do. so please help me out with that too if you can. Also, I am very new to Java so if there is an obvious solution please do not make fun of me.
import java.util.Random;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
while (true){
System.out.print("Guess a number 1-10.");
Random rand = new Random();
int n = rand.nextInt(10);
Scanner numgood = new Scanner(System.in);
String a = numgood.next();
if (a.equals(n)) {
System.out.println("Correct! Wanna do another round? (yes/no)\n");
Scanner round = new Scanner(System.in);
String r = round.next();
if (r.equals("yes")) {
System.out.print("\033[H\033[2J");
System.out.flush();
}
if (r.equals("no")) {
System.out.println("Thanks for playing! Bye!");
System.exit(0);
numgood.close();
}
else {
System.out.println("Answer with a correct statement");
round.close();
}
}
else {
System.out.println("Incorrect! Wanna do another round?\n");
Scanner round2 = new Scanner(System.in);
String e = round2.next();
if (e.equals("yes")) {
System.out.flush();
}
if (e.equals("no")) {
System.out.flush();
System.out.println("Thanks for playing! Bye!");
System.exit(0);
}
else {
System.out.println("Answer with a correct statement");
round2.close();
}
}
}
}
}
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire