我正在制作一個簡單的程式,它是一個猜謎游戲,您需要做的就是猜測亂數。只要你沒有得到正確的答案,它就會不斷地要求輸入。
我創建了兩個例外,如果輸入值高或低,則拋出這些例外。
我還需要使用另一個例外 InputMismatchException 但是當它使用它給我一個無限回圈。它不會要求用戶輸入,而是直接跳到 InputMismatchException。我如何讓它與我的自定義例外一起作業?
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int min = 1, max = 50;
final int random = (int) Math.floor(Math.random() * (max - min 1) min);
boolean status = false;
int count = 0;
while (status != true) {
try {
System.out.println("Guess a number from 1 to 50: ");
int answer = scan.nextInt();
if (answer < random) {
throw new InputTooLowException("");
} else if (answer > random) {
throw new InputTooHighException("");
} else if (answer == random) {
System.out.println("\n" "Congratulations! You got it in " count " attempt/s");
status = true;
}
} catch (InputTooLowException e) {
System.out.println("Too low. Try again.");
status = false;
count = count 1;
} catch (InputTooHighException e) {
System.out.println("Too high. Try again.");
status = false;
count = count 1;
} catch (InputMismatchException e) {
System.out.println("Invalid Input");
status = false;
}
}
}
}
uj5u.com熱心網友回復:
這個程式對我有用。再次強調,這是對例外的錯誤/非慣用用法。
我強烈推薦 Joshua Bloch 的名著“Effective Java”。第 10 章第 69 項是關于這一點的:“僅在例外情況下使用例外”。
卡內基梅隆大學軟體工程學院在他們的編碼指南中也有這一點。
現在關于無限回圈:我沒有意識到這InvalidInputException不是您的自定義例外之一,而是來自 java.util 并由scan.nextInt.
掃描儀的檔案說:
當掃描器拋出 InputMismatchException 時,掃描器不會傳遞導致例外的令牌,因此可以通過其他方法檢索或跳過它。
這意味著,如果您輸入文本而不是數字,Scanner 會通知您,但不會簡單地丟棄輸入。你必須自己處理。例如,通過呼叫next. 我hasNext在這里添加了支票。用戶可以按 Ctrl d,這意味著類似于“檔案結束”(EOF)。這將關閉輸入流。我的檢查可以防止此處出現錯誤,但是您對 的呼叫nextInt可能會拋出一個NoSuchElementException. 這應該在真正的代碼中處理,但我懷疑你的教授會注意到。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
final int min = 1, max = 50;
final int random = (int) Math.floor(Math.random() * (max - min 1) min);
boolean status = false;
int count = 0;
while (status != true) {
try {
System.out.println("Guess a number from 1 to 50: ");
int answer = scan.nextInt();
// Note that this is a _terrible_ use of Exceptions
// and _bad practice_.
if (answer < random) {
throw new InputTooLowException("");
} else if (answer > random) {
throw new InputTooHighException("");
} else if (answer == random) {
System.out.println("\n" "Congratulations! You got it in " count " attempt/s");
status = true;
}
} catch (InputTooLowException e) {
System.out.println("Too low. Try again.");
count = count 1;
} catch (InputTooHighException e) {
System.out.println("Too high. Try again.");
count = count 1;
} catch (InputMismatchException e) {
System.out.println("Invalid Input");
// Check if there actually is an invalid token
if (scan.hasNext()) {
// Discard invalid token
scan.next();
}
}
}
}
// Defining Exceptions here, but could do it in their own files.
private static class InputTooHighException extends Exception {
public InputTooHighException(String msg) { super(msg); }
}
private static class InputTooLowException extends Exception {
public InputTooLowException(String msg) { super(msg); }
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/324112.html
上一篇:“例外成功發送到服務器”事件
