ここでちょっと困っています。数学の質問で使用する桁数をユーザーに尋ねる数学プログラムを作成しています。次に、数学の問題の種類を尋ね、ユーザーに 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.");
}
}
}
}