-2

我正在创建一个数学程序,询问用户他们想使用多少位数,然后询问他们想要做什么样的数学。然后它会根据用户的答案向用户提出 10 个数学问题。然后它决定它们是对还是错并显示适当的消息。然后它会计算他们正确答案的百分比,并根据该百分比显示一条消息。

我似乎有数学逻辑工作,但一旦它循环到第二个问题,我得到一个 NullpointerException。

我以前从未遇到过这个问题,但我从四处搜索中收集到的是,这意味着我正在尝试使用一个具有空值的对象(不是 100%?)。谁能告诉我为什么这段代码第一次可以正常工作,但第二次抛出这个异常?

谢谢你的帮助。

在 assignment2.Assignment2.askQuestion(Assignment2.java:104) 在 assignment2.Assignment2.main(Assignment2.java:43) 的线程“主”java.lang.NullPointerException 中的异常

第 104 行读取

operatorText = (selectedOperator == 5) ? operators[(int) Math.floor(Math.random() * operators.length)] : operators[selectedOperator - 1];

第 43 行读取

   askQuestion(0, null, 0, 0, null, 0);

完整代码

import java.util.*;
import javax.swing.JOptionPane;

public class Problem2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        int difficulty = 1;
        String[] operators = {"plus", "minus", "times", "divided by"};
        int selectedOperator = 1;
        int correctAnswers = 0;
        int answeredTyped = 0;

        int difficultyInput = Integer.parseInt(
            JOptionPane.showInputDialog("Please choose the difficulty. " +
                    "Enter the number of digits to use in each problem."));

        if (difficultyInput > 0) {
            difficulty = difficultyInput;
        }
        int arithmeticMethod = Integer.parseInt(
            JOptionPane.showInputDialog(
                "Choose an arithmetic problem to study: 1 " +
                "= Addition Only, 2 = Subtraction Only, 3 = " +
                "Multiplication Only, 4 = Division Only, 5 = Random Problems" ));

        selectedOperator = arithmeticMethod;

        new Problem2().askQuestion(difficulty, null,
                    arithmeticMethod, arithmeticMethod,
                                operators, arithmeticMethod);

        while(answeredTyped < 10) {
            askQuestion(0, null, 0, 0, null, 0);
            if(((float)correctAnswers / answeredTyped) >= 0.75) {
                JOptionPane.showMessageDialog(null,
                    "Congratulations, you are ready to go on to the next level!");
            } else {
                JOptionPane.showMessageDialog(
                    null, "Please ask your teacher for extra help.");
            }
        }
    }

    public static boolean checkResponse (double primaryInt,
            double secondaryInt, String operatorText, double response){
        if (operatorText.equals("plus")) {
            return (primaryInt + secondaryInt) == response;
        } else if (operatorText.equals("minus")) {
            return (primaryInt - secondaryInt) == response;
        } else if (operatorText.equals("times")) {
            return (primaryInt * secondaryInt) == response;
        } else if (operatorText.equals("divided by")) {
            return (primaryInt / secondaryInt) == response;
        }
        return false;
    }

    public static String displayResponse (boolean isCorrect) {
        int randomIndex = (int) (Math.floor(Math.random() * (4 - 1 + 1)) + 1);
        switch (randomIndex) {
            case 1:
                return isCorrect ? "Very Good!" : "No. Please try again.";
            case 2:
                return isCorrect ? "Excellent!" : "Wrong. Try once more.";
            case 3:
                return isCorrect ? "Nice Work!" : "Don\'t give up!";
            case 4:
                return isCorrect ? "Keep up the good work!" : "No. Keep trying.";
        }
        return "Oops...";
    }

    public static void askQuestion(int difficulty,
            String operatorText, int selectedOperator,
                int answeredTyped, String[] operators, int correctAnswers){
        boolean correctAnswer = false;
        double primaryInt = Math.floor(Math.pow(10,
            difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1));
        double secondaryInt = Math.floor(Math.pow(10,
            difficulty-1) + Math.random() * 9 * Math.pow(10, difficulty-1));
        operatorText = (selectedOperator == 5) ? operators[(int) Math.floor(Math.random() * operators.length)] : operators[selectedOperator - 1];

        while(!correctAnswer && answeredTyped < 10) {
            double response = Double.parseDouble (
                JOptionPane.showInputDialog("How much is " +
                    primaryInt + " " + operatorText + " " + secondaryInt + "?"));
            correctAnswer = checkResponse (primaryInt,
                    secondaryInt, operatorText, response);
            JOptionPane.showMessageDialog(null, displayResponse(correctAnswer));

            answeredTyped++;

            if(correctAnswer)
                correctAnswers++;
        }
    }
}
4

1 回答 1

3

askQuestion(0, null, 0, 0, null, 0);

静态方法 askQuestion 的所有参数都是空/零,包括“运算符”。当您对“operatorText”进行松散的分配时,“operators”不可能不为空。

于 2013-09-29T02:49:54.857 回答