askQuestion()方法中的Java逻辑问题

askQuestion()方法中的Java逻辑问题,java,Java,我正在尝试创建一个Java数学培训计划。我有一个Javascript的工作版本,你们中的一些人已经帮助我将其转换为Java。它应该问用户一个困难(然后在问题中每个数字都使用这个数字)。然后它询问用户要做什么类型的数学(加法、减法、乘法、除法、随机)。然后向用户询问10个问题。当用户回答时,它会告诉他们是对还是错。如果错了,他们可以继续尝试这个问题。在10个问题的末尾,它会计算出你是否答对了其中75%以上的问题,并显示适当的回答。完整说明: 我终于让它大部分工作正常,结果发现数学本身是错误的。

我正在尝试创建一个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.");
            }
        }
    }
}
import java.util.*;
导入javax.swing.JOptionPane;
公共课堂作业2{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪输入=新扫描仪(System.in);
智力难度=1;
字符串[]运算符={“加”、“减”、“倍”、“除以”};
int-selectedOperator=1;
int=0;
int-answeredTyped=0;
int困难输入=Integer.parseInt(
JOptionPane.showInputDialog(
“请选择难度。”+
“输入每个问题要使用的位数。”);
如果(困难输入>0){
困难=困难输入;
}
int arithmethod=Integer.parseInt(
JOptionPane.showInputDialog(
“选择要学习的算术题:”+
1=仅限加法,2=仅限减法+
3=仅乘法,4=仅除法+
“5=随机问题”);
选择运算符=算术方法;
新任务2().askQuestion(
难度,空,算术方法,
算术方法,运算符,算术方法);
}
公共静态布尔校验响应(
int primaryInt,int secondaryInt,
字符串运算符(文本,浮点响应){
布尔结果=假;
开关(操作员文本){
案例“1”:
返回(primaryInt+secondaryInt)=响应;
案例“2”:
返回(primaryInt-secondaryInt)=响应;
案例“3”:
返回(primaryInt*secondaryInt)=响应;
案例“4”:
返回(primaryInt/secondaryInt)=响应;
}
返回false;
}
公共静态字符串displayResponse(布尔值isCorrect){
intrandomIndex=(int)(Math.floor(Math.random()*(4-1+1))+1);
开关(随机索引){
案例1:
返回正确吗?“非常好!”:“不,请再试一次。”;
案例2:
return是否正确?“太好了!”:“错了,再试一次。”;
案例3:
回答正确吗?“干得好!”:“不要放弃!”;
案例4:
回答正确吗?“继续努力!”:“不,继续努力。”;
}
返回“Oops…”;
}
公共静态无效问题(
int难度,字符串运算符text,
int选择运算符,int应答键入,
字符串[]运算符,整数(0){
布尔正确答案=假;
int primaryInt=(int)Math.floor(Math.pow(10,难度-1)+Math.random()*9*Math.pow(10,难度-1));
int secondaryInt=(int)Math.floor(Math.pow(10,难度-1)+Math.random()*9*Math.pow(10,难度-1));
operatorText=(selectedOperator==5)?operators[(int)Math.floor(Math.random()*operators.length)]:operators[selectedOperator-1];
而(!正确答案和回答键入<10){
float response=float.parseFloat(JOptionPane.showInputDialog(“多少钱是“+primaryInt+”+operatorText+”+secondaryInt+”)));
correctAnswer=checkResponse(primaryInt、secondaryInt、operatorText、response);
showMessageDialog(null,displayResponse(correctResponse));
回答输入++;
如果(正确答案)
正确答案++;
}
{
while(回答类型<10){
askQuestion(0,null,0,0,null,0);
}
如果((正确答案/回答类型)>=0.75){
JOptionPane.showMessageDialog(
null,“恭喜你,你准备好了”+
“进入下一个层次!”;
}否则{
JOptionPane.showMessageDialog(
null,“请向您的老师寻求额外帮助。”);
}
}
}
}

在您调用的代码中,询问您已完成的问题

new Assignment2().askQuestion(arithmeticMethod, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod);
}
但从你的方法定义来看,你应该是通过了一个新的方法,而不是难度等级

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

非常感谢,希瓦姆!看起来这一改变解决了难题,但它仍然告诉我答案是错误的。你知道吗?一般来说,我是编程新手,所以非常感谢您的帮助!这是因为您的检查响应功能。您正在比较两个双精度,这两个双精度永远不会准确,因此,计算结果始终为假。经验法则:如果你想在它上面设置条件,千万不要比较double,这样会引入不必要的bug。您可能需要计算最接近的整数或最高达2个小数点的答案,然后进行检查。好的,我将double改为integer,它似乎仍然给了我一个错误的答案。。