TP
This commit is contained in:
50
DEV2.1/TP13/01_Devinette/Main.java
Normal file
50
DEV2.1/TP13/01_Devinette/Main.java
Normal file
@@ -0,0 +1,50 @@
|
||||
import java.awt.*;
|
||||
import java.io.*;
|
||||
import java.util.Random;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Random aleatoire = new Random();
|
||||
int aDeviner = aleatoire.nextInt(100) + 1;
|
||||
|
||||
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
|
||||
String essai = "-1";
|
||||
|
||||
try {
|
||||
System.out.println("Essai n°1 : Veuillez entrer un nombre (1 à 100) : ");
|
||||
essai = console.readLine();
|
||||
} catch (IOException e) {
|
||||
System.err.println("Erreur de console.");
|
||||
}
|
||||
try {
|
||||
for (int i = 2; i != 6; i++) {
|
||||
if (Integer.parseInt(essai) > aDeviner) {
|
||||
System.out.println("-");
|
||||
}
|
||||
|
||||
else if (Integer.parseInt(essai) == aDeviner) {
|
||||
break;
|
||||
}
|
||||
|
||||
else {
|
||||
System.out.println("+");
|
||||
}
|
||||
System.out.println("Essai n°" + i + " : Veuillez entrer un nombre (1 à 100) : ");
|
||||
essai = console.readLine();
|
||||
}
|
||||
|
||||
if (Integer.parseInt(essai) == aDeviner) {
|
||||
System.out.println("Nombre trouvé.");
|
||||
}
|
||||
else {
|
||||
System.out.println("Nombre non trouvé après 5 essais.");
|
||||
}
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Erreur de console.");
|
||||
} catch (NumberFormatException e2) {
|
||||
System.err.println("Veuillez n'entrer que des nombres.");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user