g Samer:
Tengo un programa de juego de adivinanzas en el que tienes 4 intentos de adivinar un número entre 0 y 9. Quiero añadir una opción donde el programa pregunta al usuario si desea jugar de nuevo cuando el usuario supera sus 4 intentos. Este es mi código:
public static void main(String[] args) {
int nb, x;
int count = 1;
Scanner inp = new Scanner(System.in);
nb = (int) (Math.random() * 10);
System.out.print("Guess the number between 0 and 9 (4 Guesses allowed):");
x = inp.nextInt();
while (x != nb) {
if (count <= 3) {
if (x > nb) System.out.println("Lesser");
else if (x < nb) System.out.println("Bigger");
System.out.println("Wrong try another number");
x = inp.nextInt();
if (x == nb) System.out.println("Found!!");
} else {
System.out.print("you exceeded your allowed Guesses");
break;
}
count++;
}
inp.close();
}
DCR:
con sólo un leve pellizco esto le ayudará a ir.
public static void main(String[] args) {
int nb, x=-1;
int count = 0;
Scanner inp = new Scanner(System.in);
nb = (int) (Math.random() * 10);
while (x != nb) {
System.out.print("Guess the number between 0 and 9 (4 Guesses allowed):");
x = inp.nextInt();
inp.nextLine();
count++;
if (x == nb){ System.out.println("Found!!");break;}
if (x > nb) System.out.println("Lesser");
else if (x < nb) System.out.println("Bigger");
System.out.println("Wrong try another number");
if (count == 2) {
System.out.println("you exceeded your allowed Guesses");
System.out.println("would you like to play again? (input y or n)");
String newGame = inp.next();
inp.nextLine();
if(newGame.compareTo("n")==0)break;
else count = 0;
}
}
inp.close();
}
}