我正在嘗試在 Java 上創建一個測驗專案。代碼似乎運行良好。但是,我希望它僅將字串作為輸入,并且如果用戶嘗試輸入字串以外的任何內容,則會彈出一條訊息,提示他們應該輸入一個字母并讓他們再次嘗試該專案。我應該做一個 try-catch 例外,但如果我輸入除字串以外的任何內容,則不會彈出 InputMismatchException。
public class Question {
String prompt;
String answer;
public Question(String prompt, String answer) {
this.prompt = prompt;
this.answer = answer;
}
}
import java.util.Scanner;
public class App {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String q1 = "What is the hottest planet in the Solar System?\n"
"a. Mercury\nb. Venus\nc. Jupiter";
String q2 = "Who is the hottest instructor in the IT Department?\n"
"a. Sir Harly\nb. Sir Daryll\nc. Sir Rhady";
String q3 = "What is largest planet in the Solar System\n"
"a. Mercury\nb. Venus\nc. Jupiter";
String q4 = "What is the closest planet in the Sun?\n"
"a. Mercury\nb. Venus\nc. Jupiter";
String q5 = "What is the Earth's 'Twin Sister'?\n"
"a. Mercury\nb. Venus\nc. Jupiter";
String q6 = "Which among these three is a 'Gas Giant'?\n"
"a. Mercury\nb. Venus\nc. Jupiter";
String q7 = "What planet is Olympus Mons located?\n"
"a. Saturn\nb. Mars\nc. Uranus";
String q8 = "Which among these three planet doesn't have a ring?\n"
"a. Saturn\nb. Mars\nc. Uranus";
String q9 = "Which of these planet appears to spin on its side, orbiting the Sun like a rolling ball.?\n"
"a. Saturn\nb. Mars\nc. Uranus";
String q10 = "On which planet does the Great Red Spot, a long-lived enormous storm system, located?\n"
"a. Jupiter\nb. Mars\nc. Uranus";
Question [] questions = {
new Question(q1, "b"),
new Question(q2, "a"),
new Question(q3, "c"),
new Question(q4, "a"),
new Question(q5, "b"),
new Question(q6, "c"),
new Question(q7, "b"),
new Question(q8, "b"),
new Question(q9, "c"),
new Question(q10, "a")
} ;
takeTest(questions);
}
public static void takeTest(Question [] questions) {
int score = 0;
Scanner userInput = new Scanner(System.in);
for (int i = 0; i < questions.length; i ) {
System.out.println(questions[i].prompt);
String answer = userInput.next();
if(answer.equalsIgnoreCase(questions[i].answer)) {
score ;
}
}
System.out.println("Your score is: " score "/" questions.length);
}
}
uj5u.com熱心網友回復:
您想檢查答案是否只有一個字符和 a、b 或 c 之一?
private boolean isStringABC(String s){ return (s.length()==1 && (s.toLowerCase().charAt(0)=='a' || .toLowerCase().charAt(0)=='b ' || s.toLowerCase().charAt(0)=='c'); }
uj5u.com熱心網友回復:
我認為您的方法是不正確的,但是如果您真的同意測驗數字的想法,請使用正則運算式。
//Create a pattern
private static final Pattern pattern = Pattern.compile("\\d");
//testing method
public static boolean hasDigit(String input) {
Matcher matcher = pattern.matcher(input);
boolean found = matcher.find();
if (found) {
System.out.println(input " contains a digit.");
} else {
System.out.println(input " does not contain a digit");
}
return found;
}
public static void main(String[] args) throws IOException {
String good = "abc";
String bad = "a1c";
hasDigit(good);
hasDigit(bad);
}
uj5u.com熱心網友回復:
我假設這是學習 try-catch 和例外的某種家庭作業。如果是這種情況:
如果您嘗試執行不允許的操作,則函式會引發例外。要捕獲此錯誤,您可以使用 try catch 塊包圍引發例外的函式。在這種情況下,掃描器應該拋出一個錯誤,因為用戶可能沒有輸入正確的值。用戶可以在呼叫時輸入錯誤的值userInput.next();,所以這是我們想要用 try-catch 塊包圍的代碼。為了捕獲 InputMismatchException,我們在 catch 之后的括號之間放置。像這樣:
...
try {
String answer = userInput.next();
}
catch(InputMismatchException e) {
// Block of code to handle errors
}
...
然而有一個問題。如果我們查看檔案,我們可以看到 Scanner.next() 沒有拋出 InputMismatchException,所以這不會做任何事情。我可以想到三種簡單的方法來解決這個問題。
- 使用像Scanner.nextInt()這樣會拋出 InputMismatchException 的函式重寫程式。您需要將問題選項從 a、b、c 更改為 1、2、3。但是,當唯一的選項是 1、2、3 時,這仍然允許用戶輸入 489。
- 將正則運算式模式添加到您的下一個函式。請參閱Scanner.next(Pattern pattern)。如果模式不匹配,您將收到 NoSuchElementException。
- 撰寫您自己的函式,如果輸入不是 a、b 或 c,則拋出 InputMismatchException。像這樣的東西:
public static void checkAnswer(String str) {
if (str != "a" || str != "b" || str != "c") {
throw new InputMismatchException();
}
}
如果您有興趣,這里有更多關于 try-catch 塊和例外的資訊。https://www.w3schools.com/java/java_try_catch.asp
我希望這有幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/461496.html
