gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:17:14 25 4
gpt4 key购买 nike

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

enter image description here

我终于让它的大部分工作正常,却发现数学本身是错误的。有时如果我输入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.");
}
}
}
}

最佳答案

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

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

但是看看你的方法定义,你传递的是 arthmeticMethod 而不是难度级别

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

关于askQuestion() 方法中的 Java 逻辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com