您好我已经使用java创建了一个测验应用程序,但我不确定如何实现这个小部分。它要求我: 如果用户输入一个字符,即'a'
,则添加例外以捕获NumericQuestion类型上的NumberFormatException我最初尝试在presentQuestion方法的开头插入一个try catch块,但它给我带来了很多错误。我没有太多实施这些try-catch方法的经验。我知道它可能很简单,所以任何帮助都会受到欢迎。 我的代码如下所示。
package QuestionGame;
import java.util.Scanner;
public class NumericQuestionTester {
public static void main(String[] args) {
boolean userCorrect;
Scanner input = new Scanner(System.in);
NumericQuestion quThree = new NumericQuestion("What is the answer to 5 divided by 3?", "1.67", 2, 0.03, 0.07);
presentQuestion(input, quThree);
input.close();
}
public static void presentQuestion(Scanner input, NumericQuestion q) {
boolean userCorrect;
q.displayQuestion();
System.out.println("Enter your answer: ");
String answer = input.nextLine();
userCorrect = q.checkAnswer(answer);
if (userCorrect){
System.out.println("Correct! The answer is : " + q.getAnswer() + " and you have a score of : " + q.getMark());
} else {
System.out.println(answer + " is incorrect. You scored zero.");
}
System.out.println();
}
}
package QuestionGame;
public class NumericQuestion extends Question {
//instance variables
private double ansNumeric;
private double positiveRange;
private double negativeRange;
//constructor
public NumericQuestion(String quText, String answer, int mark, double positiveRange, double negativeRange) {
super(quText, answer, mark);
this.ansNumeric = Double.parseDouble(answer);
this.positiveRange = positiveRange;
this.negativeRange = negativeRange;
}
//accessors & mutators
public double getAnsNumeric() {
return ansNumeric;
}
public void setAnsNumeric(double ansNumeric) {
this.ansNumeric = ansNumeric;
}
//methods
public boolean checkAnswer (String answer) {
double answerDouble = Double.parseDouble(answer);
if (answerDouble > (this.ansNumeric + this.positiveRange)) {
return false;
} else if (answerDouble < (this.ansNumeric - this.negativeRange)) {
return false;
} else {
return true;
}
}
}
答案 0 :(得分:3)
除exception
处理之外,我建议您添加持久性检查,而用户不会放置预期的整数答案。
do{
System.out.println("You Numeric answer, please")
String answer = input.nextLine();
}while !answerIsNumeric(answer);
userCorrect = q.checkAnswer(answer);
public boolean answerIsNumeric(String answer){
try{
Double.parseDouble(answer);
}catch(NumberFormatException e){
return false;
}
return true;
}
答案 1 :(得分:2)
根据应用程序设计,有很多方法可以实现这一目标。我会在这里向您展示一个 - 如果不适用,请提供更多细节,不过我想这就是您要找的东西
public static void presentQuestion(Scanner input, NumericQuestion q) {
boolean userCorrect = false;
boolean isAnswerNumeric = false;
String answer = null;
while(! isAnswerNumeric){
q.displayQuestion();
System.out.println("Enter your answer (numeric only) : ");
answer = input.nextLine();
try{
Double.parseDouble(answer);
isAnswerNumeric = true;
}catch(NumberFormatException nfe){
// do nothing in this case.. if the number format is correct,
// the isAnswerNumeric will be set to true
}
}
userCorrect = q.checkAnswer(answer);
if (userCorrect){
System.out.println("Correct! The answer is : " + q.getAnswer() + " and you have a score of : " + q.getMark());
} else {
System.out.println(answer + " is incorrect. You scored zero.");
}
System.out.println();
}
您可以调整代码以显示错误消息或您想要在catch块中执行的任何操作。在这种情况下,作为一个简单的例子,问题会被再次询问,直到答案是数字
答案 2 :(得分:0)
您可能只需要将answer
字符串解析为Float.parseFloat
的数字并抓住NumberFormatException
。
示例:
String answer = "3.14";
try {
float numericAnswer = Float.parseFloat(answer);
System.out.println("numericAnswer=" + numericAnswer);
} catch (NumberFormatException e) {
System.err.println(answer + " is not a number.");
}
如果通过添加异常来捕获NumberFormatException - 这句话没有多大意义 - 它们意味着抛出异常,只需添加catch块
throw new WhateverException(answer + " is not a number.");
(f.ex。你可以抛出RuntimeException
)。