Pour mon avis
S'il y a "Enter 0 to 2", "Enter 0 to 2" s'affiche. S'il y a "Saisir une chaîne de caractères", "Pas une valeur entière" s'affiche.
Parce que je pourrais utiliser le scanner hasNext au lieu d'utiliser try-catch Enregistrez qu'il existe un tel moyen.
hello.java
void getJudgeHand() {
while (true) {
System.out.println("commander? 0:Goo 1:Par 2:Choki");
if (scanner.hasNextInt()) {
myHand = scanner.nextInt();
if (myHand >= 0 && myHand <= 2) break;
}
else {
scanner.next();
System.out.println("Pas une valeur entière");
}
System.out.println("Veuillez saisir de 0 à 2");
}
}
Méthode try-catch
hello.java
void getJudgeHand() {
while (true) {
try {
System.out.println("commander? 0:Goo 1:Par 2:Choki");
this.myHand = scanner.nextInt();
if (this.myHand <= 2 && this.myHand >= 0) { //Pause si 2 ou moins et 0 ou plus
break;
}
} catch (InputMismatchException e) {
System.out.println("Pas une valeur entière");
scanner.next(); //Supprimer l'entrée
}
System.out.println("Veuillez saisir de 0 à 2");
}
}
Recommended Posts