askQuestion()方法中的Java Logic问题

时间:2013-09-28 04:33:52

标签: java

我正在尝试创建一个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.");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

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

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