【问题标题】:Trying to use a while or do while loop to run a basic math game with certain conditions尝试使用 while 或执行 while 循环来运行具有特定条件的基本数学游戏
【发布时间】:2015-11-10 01:48:08
【问题描述】:

代码的主体应该一直运行到用户输入了至少 10 个问题,输入的问题不超过 20 个,并且直到学生的 >正确百分比为 85% 或更高。

这不是全部,但我认为只有 while 循环最麻烦。我已经尝试了多种组合以使代码按上述方式运行,包括我给出的方式。我已经测试了所有变量,并且它们确实在程序运行时保持跟踪,但是我怎样才能让这段代码像规范所说的那样运行?

到目前为止,我有这个:

//Declaring int variables to store values of correct/wrong problems
    int numCorrect = 0;
    int numWrong = 0;
    int totalQuestions = 0;
    double percent = 0;
    int subNumCorrect = 0;
    int subNumWrong = 0;
    int addNumCorrect = 0;
    int addNumWrong = 0;
    //Storing the users answers as integers, after asking them for a          solution to a random problem
    //Then looping this code 20 times   
    while((totalQuestions <= 5 && percent >= 85.0 || totalQuestions < 10)) {
        Question theQuestion = new Question();
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is the result?" + "\n" + theQuestion.toString());
        int result = scanner.nextInt();

        // Checks if the question is addition/subtraction and correct/wrong, then increases the variables 1 

        if((theQuestion.getOperator() == '+') && (result == theQuestion.determineAnswer()))
            addNumCorrect += 1;
        else if(theQuestion.getOperator() == '+' && (result != theQuestion.determineAnswer()))
            addNumWrong += 1;

        if((theQuestion.getOperator() == '-') && (result == theQuestion.determineAnswer()))
            subNumCorrect += 1;
        else if((theQuestion.getOperator() == '-') && (result != theQuestion.determineAnswer()))
            subNumWrong += 1;

        //Determines if the users answer is correct or not, and displays a message accordingly
        //As well as keeping count of how many correct/wrong answers are attempted by the user and storing them
        //in the variable numCorrect/numWrong
        if(result == theQuestion.determineAnswer()) {
            numCorrect += 1; 
            System.out.println("Congratulations, you got it correct!");
        }
        else if(result != theQuestion.determineAnswer()) {
            numWrong += 1;
            System.out.println("The correct answer for " + theQuestion.toString()
                + " is " + theQuestion.determineAnswer());

        }
        //Increasing value of totalQuestions by one every loop
        totalQuestions++;

        //Multiplying the number of correct problems by 100 and dividing the product by the number of
        //total questions. Then rounding the quotient to get the percentage of correct answers
        percent = Math.round((double)(numCorrect * 100) / (totalQuestions));
    }
    //Progress report displaying how many addition and subtraction problems the user got correct and wrong
    //Then determines the percentage of correct problems the user entered


    System.out.println("\nProgress Report:" + "\nAddition:\nYou got " + addNumCorrect
        + " correct and " + addNumWrong + " incorrect.");
    System.out.println("Subtraction:\nYou got " + subNumCorrect
        + " correct and " + subNumWrong + " incorrect.");
    System.out.println("Number of problems: " + totalQuestions);
    System.out.println("The percent correct: " + percent + "%");

}

}

【问题讨论】:

  • 那么到底是什么问题,循环停止了吗?
  • 在这种情况下,我使用 10 和 5 而不是 20 和 10,因为它使我在测试时更容易和更快。所以当我运行它时,它应该停止时继续运行(如果正确百分比高于 85% 并且用户输入了至少 5 个问题,它应该停止)。对于在 while 循环中使用什么条件来获得问题开头所述的条件,我感到非常困惑。
  • @JawadLeWywadi 我在哪里失踪()?
  • 如果他达到 20 个问题但结果低于 85 会怎样?我的意思是你需要它做什么。
  • 问题来自于你的 85% 不清楚如果你有超过 20 个问题而你没有 85% 会发生什么......

标签: java eclipse while-loop coding-style do-while


【解决方案1】:

给你:

while((totalQuestions < 10) || (totalQuestions >= 10 && totalQuestions <= 20 && percent < 85.0)) {

【讨论】:

  • 还是不行。当我运行此程序并正确回答 10 个问题并且有 85% 或更高的正确答案时,游戏应该停止,但即使我回答所有问题都正确它也不会继续。只要我有 85% 或更多的正确率,它就应该停止 10-20 个问题。无论如何,游戏应该以 20 个问题结束。谢谢你的帮助顺便说一句
  • 别担心!您可以发布计算百分比并添加到 totalQuestions 的代码吗?
  • 我刚刚发布了其余部分
  • 您能否将 System.out.println 打印的问题总数、正确回答的数量、错误回答的数量和百分比放在 while 循环中。运行程序,并在此处粘贴您的输出。
【解决方案2】:

在 Kabir 的回答之上,我只需将结构更改为:

    int numCorrect = 0;
    int numWrong = 0;
    int totalQuestions = 0;
    double percent = 0;
    int subNumCorrect = 0;
    int subNumWrong = 0;
    int addNumCorrect = 0;
    int addNumWrong = 0;
    do{        Question theQuestion = new Question();
        Scanner scanner = new Scanner(System.in);
        System.out.println("What is the result?" + "\n" + theQuestion.toString());
        int result = scanner.nextInt();

        // Checks if the question is addition/subtraction and correct/wrong, then increases the variables 1 };
    ...
    while(totalQuestions >= 10 && totalQuestions <= 20 && percent >= 85.0);

然后,如果您想添加两个 if 语句来告诉您是 85% 打破了循环,还是 totalQuestions 超过 20。

对不起,如果答案是丑陋的第一个计时器就在这里=)。

【讨论】:

  • 感谢您的回复,但我尝试了这种情况,它跳过了整个身体。也是这里的新手。祝你好运
【解决方案3】:

这里有一个解决方案。它总是会问至少 10 个问题,但永远不会超过 20 个。在这之间,如果百分比变得大于 85,控制将退出循环。

它使用一个 do-while 循环来创建一个 Question 类型的对象(您已经完成了)。然后立即增加问题的数量。请注意,do-while 循环的主体将始终执行至少一次。

接下来,它会提示用户输入答案并将其存储(就像您的代码所做的那样,尽管声明放在循环之外)。

在您提供用于评估用户回答的代码后,它会计算正确问题的百分比并存储该值。请注意,表达式需要乘以 1.0 以避免整数除法(这会截断结果)。然后将该计算值与逻辑“或”中的问题总数一起使用,以查看 do-while 循环是否应该继续。

在循环体的末尾,它会检查总问题数是否等于或超过 20,这是根据您的描述允许的最大问题数。如果问题的数量大于或等于 20,则程序使用 break 语句退出循环。根据您的描述,我理解此要求是正确的。

在 do-while 循环之外,它会报告问题的总数和正确答案的百分比,以求简洁。为了清楚起见,添加了评论。我希望这有帮助。祝你好运。

    //Declaring int variables to store values of correct/wrong problems
    int numCorrect = 0;
    int numWrong = 0;
    int totalQuestions = 0;
    double percent = 0.0;
    int subNumCorrect = 0;
    int subNumWrong = 0;
    int addNumCorrect = 0;
    int addNumWrong = 0;

    /* Declare a scanner once here, we'll re-use it in the loop */
    Scanner scan = new Scanner( System.in );

    /* Declare a variable to hold the user's input, we'll re-use it too */
    int result = 0;

    //Storing the users answers as integers, after asking them for a solution to a random problem
    //Then looping this code 20 times   
    do{    
        /* Create a new question object */
        Question theQuestion = new Question();

        /* Increment the total number of questions */
        totalQuestions++;

        /* Prompt the user for the question, and obtain the response */
        System.out.print("What is the result? " + theQuestion.toString() + " " );
        result = scan.nextInt();

        // Checks if the question is addition/subtraction and correct/wrong, then increases the variables
        if((theQuestion.getOperator() == '+') && (result == theQuestion.determineAnswer()))
            addNumCorrect += 1;
        else if(theQuestion.getOperator() == '+' && (result != theQuestion.determineAnswer()))
            addNumWrong += 1;

        if((theQuestion.getOperator() == '-') && (result == theQuestion.determineAnswer()))
            subNumCorrect += 1;

        else if((theQuestion.getOperator() == '-') && (result != theQuestion.determineAnswer()))
            subNumWrong += 1;    

        /* Calculate the percentage of correct answers */
        percent = ( (addNumCorrect + subNumCorrect) * 1.0 / totalQuestions );
        System.out.println( "Percent is: " + percent );

        /* Exit the loop if the total number of questions equals or exceeds 20 */
        if( totalQuestions >= 20 )
            break;            

    } while( totalQuestions < 10 || percent < 0.85 );

    /* Report the total number of questions asked and the correct percentage */
    System.out.println();
    System.out.println( "Total number of questions: " + totalQuestions );
    System.out.println( "Percent correct answers: " + percent );

【讨论】:

    【解决方案4】:

    如你所说:

    代码主体应该一直运行,直到用户输入至少 10 个问题,输入的问题不超过 20 个,并且学生的正确率达到 85% 或更高。

    基于此,您的 while 循环应如下所示:

    while(totalQuestions >= 10 && totalQuestions <= 20 && percent >= 85.0) {
    

    【讨论】:

    • 感谢您的快速回复。我刚刚尝试过,但输出使用您提供的条件完全跳过了正文。
    猜你喜欢
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2011-01-09
    • 2014-10-30
    • 2022-08-07
    • 2021-05-24
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多