Java中的数学计算不正确

Java中的数学计算不正确,java,Java,我这里有点麻烦。我正在制作一个数学程序,要求用户在数学问题中使用的位数。然后,它询问数学问题的类型,并向用户询问10个问题。它检查答案是否正确,并显示相应的消息。如果错误,用户可以永远重试该问题。然后计算他们的得分是否超过75%,并显示一条消息 所有的弹出窗口都很好,但不管出于什么原因,它总是告诉我,无论我输入什么,答案都是错误的。我试图确保所有的变量类型都匹配(双倍),这样响应就不会有任何转换问题,但我是Java新手,所以我不知道我是否正确地做到了这一点 你们有没有可能发现我代码的问题? 非常

我这里有点麻烦。我正在制作一个数学程序,要求用户在数学问题中使用的位数。然后,它询问数学问题的类型,并向用户询问10个问题。它检查答案是否正确,并显示相应的消息。如果错误,用户可以永远重试该问题。然后计算他们的得分是否超过75%,并显示一条消息

所有的弹出窗口都很好,但不管出于什么原因,它总是告诉我,无论我输入什么,答案都是错误的。我试图确保所有的变量类型都匹配(双倍),这样响应就不会有任何转换问题,但我是Java新手,所以我不知道我是否正确地做到了这一点

你们有没有可能发现我代码的问题? 非常感谢您的帮助。谢谢收看

同样感谢你们中已经在这个项目的其他问题上帮助过我的人,我仍然在学习Java

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

/**
 * @author Tyler
 *
 */
public class Program {

        /**
         * @param args
         */
        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 Program().askQuestion(difficulty, null, arithmeticMethod, arithmeticMethod, operators, arithmeticMethod); }





        public static boolean checkResponse (double primaryInt, double secondaryInt, String operatorText, double response){
              if (operatorText.equals("1")){
                  return (primaryInt + secondaryInt) == response;
              }
              else if (operatorText.equals("2")){
                  return (primaryInt - secondaryInt) == response;
              }
              else if (operatorText.equals("3")){
                  return (primaryInt * secondaryInt) == response;
              }
              else if (operatorText.equals("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;
                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++;
                }

        {

        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;
/**
*
*/
/**
*@作者泰勒
*
*/
公共课程{
/**
*@param args
*/
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪输入=新扫描仪(System.in);
智力难度=1;
字符串[]运算符={“加”、“减”、“倍”、“除以”};
int-selectedOperator=1;
int=0;
int-answeredTyped=0;
int-difficultyInput=Integer.parseInt(JOptionPane.showInputDialog(“请选择难度,输入每个问题要使用的位数”);
如果(困难输入>0){
困难=困难输入;
}
int arithmaticmethod=Integer.parseInt(JOptionPane.showInputDialog(“选择要研究的算术问题:1=仅加法,2=仅减法,3=仅乘法,4=仅除法,5=随机问题”);
选择运算符=算术方法;
新程序().askQuestion(难度、空值、算术方法、算术方法、运算符、算术方法);}
公共静态布尔checkResponse(双primaryInt、双secondaryInt、字符串运算符text、双响应){
if(运算符文本等于(“1”)){
返回(primaryInt+secondaryInt)=响应;
}
else if(运算符文本等于(“2”)){
返回(primaryInt-secondaryInt)=响应;
}
else if(运算符文本等于(“3”)){
返回(primaryInt*secondaryInt)=响应;
}
else if(运算符文本等于(“4”)){
返回(primaryInt/secondaryInt)=响应;
}
返回false;
}
公共静态字符串displayResponse(布尔值isCorrect){
intrandomIndex=(int)(Math.floor(Math.random()*(4-1+1))+1);
开关(随机索引){
案例1:
返回正确吗?“非常好!”:“不,请再试一次。”;
案例2:
return是否正确?“太好了!”:“错了,再试一次。”;
案例3:
回答正确吗?“干得好!”:“不要放弃!”;
案例4:
回答正确吗?“继续努力!”:“不,继续努力。”;
}
返回“Oops…”;
}
公共静态void askQuestion(int难度、字符串运算符或文本、int selectedOperator、int answeredTyped、字符串[]运算符、int更正答案){
布尔正确答案=假;
double primaryInt=Math.floor(Math.pow(10,难度-1)+Math.random()*9*Math.pow(10,难度-1));
double secondaryInt=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){
double response=double.parseDouble(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){
showMessageDialog(null,“恭喜,您已经准备好进入下一个级别了!”);
}
否则{
showMessageDialog(null,“请向您的老师寻求额外帮助”);
}
}
}
}

您正在进行的是
整型
除法,结果是
整型
,而不是
浮点
双精度
值。例如:

int a = 5;
int b = 2;
System.out.println(a / b); // will print 2
System.out.println((double)a / b); // will print 2.5

我把这段代码放到Eclipse中,它向我抛出了8个红色标志和5个黄色标志。大多数危险信号都与方法“pow”和“random”有关,因为“Math”没有定义。你能试着打电话给你没有写过的东西吗

        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];

如果您实际尝试调用java Math方法,可以通过将类名更改为标准“Math”以外的其他名称来解决此问题。

当checkResponse找不到运算符时,是否尝试引发异常?我不明白为什么一个无效的运算符输入会产生与出错问题相同的结果

若你们有那个么多,我希望你们会看到函数中的If语句并没有一个是真的……为什么你们要把它和“1”、“2”、“3”和“4”进行比较,而事实并非如此
        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];