0

我正在创建一个数学程序,询问用户他们想使用多少位数,然后询问他们想要做什么样的数学。然后它会根据用户的答案向用户提出 10 个数学问题。然后它决定它们是对还是错并显示适当的消息。然后它会计算他们正确答案的百分比,并根据该百分比显示一条消息。

问题是程序在 10 个问题后永远不会停止,并且永远不会计算百分比或显示最终消息。我相信这至少部分发生,因为我没有从“askQuestion”方法传递新的“answeredTyped”值。有人可以解释一下我如何可以轻松地将这个新值传递回 main 方法,使其停止在 10 处吗?或者也许告诉我还有什么让它无法正常工作?

我已经问了很多关于这个程序的问题,所以我感谢大家耐心等待我学习 Java 并且对这门语言知之甚少。

感谢您的任何帮助!(我也为这里的任何错误格式道歉!改变了大约一百万件事后,它变得丑陋了)

import java.util.Scanner;

import javax.swing.JOptionPane;

public class Assignment2 {
    public static int answeredTyped = 0;
    public static int correctAnswers = 0;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int difficulty = 1;
        String[] operators = { "plus", "minus", "times", "divided by" };
        int selectedOperator = 1;



        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, selectedOperator,
                answeredTyped, operators, correctAnswers);

        while (answeredTyped < 10) {
            askQuestion(difficulty, null, selectedOperator, answeredTyped,
                    operators, correctAnswers);
            answeredTyped++;


            if (answeredTyped>= 10) {
                if (((float) 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.");
                }
        }
    }
    }

    public static boolean checkResponse(double primaryInt, double secondaryInt,
            String operatorText, double response) {
        if (operatorText.equals("plus")) {
            return (primaryInt + secondaryInt) == response;
        } else if (operatorText.equals("minus")) {
            return (primaryInt - secondaryInt) == response;
        } else if (operatorText.equals("times")) {
            return (primaryInt * secondaryInt) == response;
        } else if (operatorText.equals("divided by")) {
            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];


            double response = Double.parseDouble(JOptionPane
                    .showInputDialog("How much is " + primaryInt + " "
                            + operatorText + " " + secondaryInt + "?"));
            correctAnswer = checkResponse(primaryInt, secondaryInt,
                    operatorText, response);
            JOptionPane.showMessageDialog(null, displayResponse(correctAnswer));
 if (correctAnswer)
                correctAnswers++;

            }

        }
4

2 回答 2

0

你有两个 while 循环,所以它会调用 askQuestion() 十次,然后是 askQuestion 自己的 while 循环:

主要的()

while (answeredTyped < 10) {

问问题()

while (!correctAnswer && answeredTyped < 10) {

尝试仅使用一个 while 循环,并将对答案的评估放在 askQuestion() 方法之外

于 2013-09-29T04:25:59.330 回答
0

您必须在 main 方法的 while 循环中增加 AnswerType,而不是在另一个方法中,因为您没有将变量声明为全局变量

于 2013-09-29T04:28:47.437 回答