gpt4 book ai didi

java - Java 中的数学计算不正确

转载 作者:行者123 更新时间:2023-12-01 07:29:44 25 4
gpt4 key购买 nike

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

所有弹出窗口都工作得很好,但无论出于什么原因,它总是告诉我,无论我输入什么,答案都是错误的。我试图确保所有变量类型都匹配( double ),以便响应不会出现任何转换问题,但我是 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.");
}
}
}
}

最佳答案

您正在进行整数除法,结果是整数,而不是浮点 double 值。例如:

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

关于java - Java 中的数学计算不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072580/

25 4 0
文章推荐: python - 如何从数字的数字生成单链表?
文章推荐: javascript - 如何访问 `
文章推荐: Jquery 图像淡出
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com