-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercice3.java
49 lines (40 loc) · 1.41 KB
/
Exercice3.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
import java.util.Random;
import java.util.Scanner;
public class Exercice3 {
public static void main(String[] args) {
long start = System.nanoTime();
Random random = new Random();
int userGuess = -1;
String buffer;
Scanner sc;
int randomInt = random.nextInt(0, 1000);
while(userGuess != randomInt) {
System.out.print("Devinez le nombre: ");
sc = new Scanner(System.in);
buffer = sc.nextLine();
userGuess = Integer.parseInt(buffer);
if (userGuess > randomInt) {
System.out.println("C'est moins!");
} else if (userGuess < randomInt) {
System.out.println("C'est plus!");
}
}
long end = System.nanoTime();
long time = end - start;
System.out.println("Bravo, le nombre était bien " + randomInt + ", il vous a fallu " + formatTime(time));
}
public static String formatTime(long time) {
long seconds = time / 1000000000;
if (seconds > 60) {
int minutes = (int) (seconds / 60);
long reste = seconds - (minutes * 60L);
if (minutes == 1) {
return minutes + " min et " + reste + " secs";
} else {
return minutes + " mins et " + reste + " secs";
}
} else {
return seconds + " secs";
}
}
}