java/project/juego_de_adivinanza_de_numeros/Main.java

66 lines
2.4 KiB
Java

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Random;
public class Main {
public static void main(String[] args) {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
Random random = new Random();
try {
System.out.printf("Ingrese el numero maximo del juego [Adivina el numero]: ");
String inputDelUsuario = bufferedReader.readLine();
int inputDelUsuarioToInt = Integer.parseInt(inputDelUsuario);
System.out.printf("%n");
int numeroRandom = random.nextInt(inputDelUsuarioToInt) + 1;
Adivina adivina = new Adivina(numeroRandom);
adivina.jugar();
} catch (IOException e) {
System.out.println("Ocurrio un error de entrada/salida:" + e.getMessage());
return;
} catch (NumberFormatException e) {
System.out.println("Por favor, ingresa solo numeros validos.");
return;
}
}
}
class Adivina {
int index;
Adivina(int index) {
this.index = index;
}
public void jugar() {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
try {
while (true) {
System.out.printf("Adivina el numero: ");
String inputDelUsuario = bufferedReader.readLine();
int inputDelUsuarioToInt = Integer.parseInt(inputDelUsuario);
if (inputDelUsuarioToInt == index) {
System.out.printf("Felicidades acertastes el numero es => [%d]%n", inputDelUsuarioToInt);
break;
}
if (inputDelUsuarioToInt < index) {
System.out.printf("Incorrecto [%d] es menor.%n%n", inputDelUsuarioToInt);
continue;
}
if (inputDelUsuarioToInt > index) {
System.out.printf("Incorrecto [%d] es mayor.%n%n", inputDelUsuarioToInt);
continue;
}
}
} catch (IOException e) {
System.out.println("Ocurrio un error de entrada/salida:" + e.getMessage());
} finally {}
}
}