我有一个测验分级器,它必须从txt文件中读取,我已经尝试了所有可能的知识组合。当我运行程序时,最后一个问题是“2的平方根是什么?”我输入1.41(这是正确答案,我也将答案格式化为2个小数点),程序说答案不正确。
这是我的代码
公共课测验;
public void read(Scanner in) {
questionList = new ArrayList<>();
Question qu = new Question();
while (in.hasNextLine()) {
double num;
type = in.nextLine();
question = in.nextLine();
switch (type) {
case "N":
num = Double.parseDouble(in.nextLine());
qu = new Question(type, question, num);
break;
}
questionList.add(qu);
}
}
public ArrayList<Question> getQuestions() {
return questionList;
}
public boolean[] checkAnswers(ArrayList<String> answers) {
boolean[] quizAnswers = new boolean[questionList.size()];
char ch;
for (int i = 0; i < questionList.size() - 1; i++) {
Question qu = questionList.get(i);
ch = qu.getMyType().charAt(0);
else if (ch == 'N') {
DecimalFormat df = new DecimalFormat("#.##");
double quizNumAnswer = qu.getMyNumAnswer();
String formattedAnswer = df.format(quizNumAnswer);
System.out.println(formattedAnswer);
if(answers.get(i).equals(formattedAnswer))
quizAnswers[i] = true;
else
quizAnswers[i] = false;
}
return quizAnswers;
}
}
公共课问题;
private String typeOfQuestion;
private String quizQuestion;
private double myNumAnswer;
public Question(String type, String question, double numAnswer) {
typeOfQuestion = type;
quizQuestion = question;
myNumAnswer = numAnswer;
}
public String getMyType() {
return typeOfQuestion;
}
public double getMyNumAnswer() {
return myNumAnswer;
}
@Override
public String toString() {
char ch = typeOfQuestion.charAt(0);
switch (ch) {
case 'N':
return typeOfQuestion + " " + quizQuestion;
}
return null;
}
public String getText() {
return toString();
}
public String getAnswer() {
char ch = typeOfQuestion.charAt(0);
String answer = null;
switch (ch) {
case 'N':
DecimalFormat df = new DecimalFormat("#.##");
String stringAnswer = df.format(myNumAnswer);
answer = stringAnswer;
break;
}
return answer;
}
我的输出
2的平方根是什么?
正确答案:1.41
你的回答:1.41
你的答案不对。
我如何比较它们并得到正确的答案,需要一些线索!