forked from mouredev/retos-programacion-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asjordi.java
80 lines (65 loc) · 2.39 KB
/
asjordi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.Random;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class MathRiddle {
static boolean gameOn = true;
static int correctAnswers = 0;
static int num1Digits = 1;
static int num2Digits = 1;
static Random random = new Random();
public static void main(String[] args) {
while (gameOn) {
int num1 = randomInt(num1Digits);
int num2 = randomInt(num2Digits);
char operation = randomOperation();
int result;
if (operation == '+') result = num1 + num2;
else if (operation == '-') result = num1 - num2;
else if (operation == '*') result = num1 * num2;
else {
while (num2 == 0) num2 = randomInt(num2Digits);
result = num1 / num2;
}
String answer = inputWithTimeout(num1, num2, operation);
if (!gameOn) break;
else if (answer.equals(Integer.toString(result))) {
System.out.println("Respuesta correcta!");
correctAnswers++;
if (correctAnswers % 5 == 0) {
if (correctAnswers % 2 == 0) num2Digits++;
else num1Digits++;
}
} else {
System.out.println("Respuesta incorrecta!");
gameOn = false;
}
}
System.out.println("Juego finalizado. Has acertado " + correctAnswers + " cálculos.");
}
private static int randomInt(int digits) {
return random.nextInt((int) Math.pow(10, digits));
}
private static char randomOperation() {
char[] operations = {'+', '-', '*', '/'};
return operations[random.nextInt(operations.length)];
}
private static String inputWithTimeout(int num1, int num2, char operation) {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("\n¡El tiempo ha finalizado! Pulsa enter.");
gameOn = false;
}
}, 3000);
String answer;
try {
System.out.print("¿Cuál es el resultado de " + num1 + " " + operation + " " + num2 + "? ");
answer = new Scanner(System.in).nextLine();
} finally {
timer.cancel();
}
return answer;
}
}