0

我有一个测验评分者作业,它必须从 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

你的回答不正确。

我怎样才能比较它们并得到正确的答案,需要一些线索!

4

0 回答 0