2

我正在尝试创建一个 Java 数学培训程序。我有一个 Javascript 的工作版本,你们中的一些人已经帮助我将它转换为 Java。它应该问用户一个困难(然后使用问题中每个数字的数字数量)。然后它会询问用户要进行哪种类型的数学运算(加法、减法、乘法、除法、随机)。然后它会问用户 10 个问题。当用户回答时,它会告诉他们他们是对还是错。如果错了,他们可以继续尝试这个问题。在 10 个问题结束时,它会计算您是否答对了超过 75% 的问题并显示适当的回答。完整说明:

在此处输入图像描述

我终于让它大部分工作正常,只是发现数学本身是错误的。有时如果我输入2的难度,它只会给出1位数字(它基本上不会正确计算难度)。此外,它总是告诉我我的数学是错误的。你们有没有机会发现逻辑有什么问题?

谢谢你的帮助。

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

public class Assignment2 {

    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 Assignment2().askQuestion(
                difficulty, null, arithmeticMethod,
                arithmeticMethod, operators, arithmeticMethod); 
    }

    public static boolean checkResponse (
            int primaryInt, int secondaryInt,
                String operatorText, float response){
        boolean result = false;
        switch (operatorText) {
            case "1":
                return (primaryInt + secondaryInt) == response;
            case "2":
                return (primaryInt - secondaryInt) == response;
            case "3":
                return (primaryInt * secondaryInt) == response;
            case "4":
                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;
        int primaryInt = (int) Math.floor(Math.pow(10,  difficulty-1) + Math.random() * 9 * Math.pow(10,  difficulty-1));
        int secondaryInt = (int) 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) {
            float response = Float.parseFloat (JOptionPane.showInputDialog("How much is " + primaryInt + " " + operatorText + " " + secondaryInt + "?")); 
            correctAnswer = checkResponse (primaryInt, secondaryInt, operatorText, response);
            JOptionPane.showMessageDialog(null, displayResponse(correctAnswer));

            answeredTyped++;

            if(correctAnswer)
            correctAnswers++;
        }
        {
            while(answeredTyped < 10) {
                askQuestion(0, null, 0, 0, null, 0);
            }
            if((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.");
            }
        }
    }
}
4

1 回答 1

2

在您调用 askQuestion 的代码中,您已完成

new Assignment2().askQuestion(arithmeticMethod, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod);
}

但是查看您的方法定义应该是,您通过的是 arthmeticMethod 而不是难度级别

new Assignment2().askQuestion(difficulty, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod);
}
于 2013-09-28T04:42:07.683 回答