我在这里遇到了一些麻烦。我正在制作一个数学程序,询问用户在数学问题中使用的位数。然后它询问数学问题的类型,并询问用户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.");
}
}
}
}
答案 0 :(得分:3)
您正在进行integer
除法,这会产生integer
,而不是float
或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
答案 1 :(得分:2)
当checkResponse无法找到运营商时,您是否尝试过抛出异常?我不明白为什么无效的操作符输入应该与得到错误的问题具有相同的结果。
如果你有这个,我希望你会看到该函数中的if语句都没有解析为true ...为什么要比较“1”,“2”,“3”和“ 4“,何时不是您如何定义操作员文本?
答案 2 :(得分:2)
我通过eclipse调试器运行你的程序,问题在于checkResponse方法。 operatorText字符串是从您的数组{“plus”,“minus”,“times”,“divide by”}填充的,但是您正在检查operatorText.equals(“1”)等。这意味着您的检查响应方法都没有实际上正在运行,并且该方法将始终只是返回false,这解释了您正在经历的行为。将它更改为operatorText.equals(“plus”),你应该好好去。
提示:使用像Eclipse这样的IDE并设置一些断点可以很容易地解决这类问题。
答案 3 :(得分:0)
if(((float)correctAnswers / answeredTyped) >= 0.75)
答案 4 :(得分:0)
我将此代码放入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”以外的其他名称来解决此问题。