java - Do-while 循环未打印正确的变量

标签 java arrays if-statement

我正在做一个编程任务,询问用户一些问题,如果用户得到错误/正确的答案也没关系,随机数生成器用于确定他们对该问题的分数。然而,如果他们选择“不可能的答案”(这是不可能的事情,他们的分数将重置为 0。

当我运行代码时,

如果我在第一个问题上得到 23 分,在第二个问题上得到 0 分,那么我在第二个问题上的得分应该重置为 0,但最终总得分显示为 23。

如有任何帮助,我们将不胜感激

import java.util.Scanner;

public class Main {
    public static double randnum(){
        int random = (int)(Math.random() * 50 + 1);
        return random;
    }


    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {

        int score1 = 0;
        int score2 = 0;
        int score3 = 0;
        int totalscore = 0;

        final double NumberofQuestions = 2;

        String[][] questions ={
            {"What is the largest bone in the human body? ", "\n Choose 1 for Femur \n Choose 2 for Tibia \n Choose 3 for Palatine Bone \n Choose 4 for Tongue  ", "1"},
            {"What is the capital of Albania? ! ","\n Choose 1 for Shkoder \n Choose 2 for Tirana \n Choose 3 for Durres \n Choose 4 for Rome ", "2"}
        };

        String[] Answers = new String[(int) NumberofQuestions];

        int x=0;
        do
        {
            System.out.print("" + (x+1) + ". " + questions[x][0] + "   "+questions[x][1]);
            Answers[x] = String.valueOf(input.nextInt());
            Answers[x].toLowerCase();

            if(questions[x][2].equals(Answers[x])) {
                score1 = (int) randnum();
                System.out.println("Correct: " + score1 + " points");
                totalscore = totalscore + score1;
            }
            if (Answers[x].equals("4")){
                System.out.println("\n Thats a impossible answer! The right answer is "+questions[x][2]);
                totalscore = 0;
                score2 = 0;
                System.out.println(score2 + " points");

            } else {
                System.out.println("\nIncorrect. The right answer is "+questions[x][2]);
                score3 = (int) randnum();
                System.out.println(score3 + " points");
                totalscore = totalscore + score3;
            }

            System.out.print("\n");
            x++;
        } while(x < NumberofQuestions); //close outer loop

        totalscore = score1 + score2 + score3;
        System.out.println("\n\t\tYou got " + totalscore + " points !\n\n\n");

        System.exit(0);
    }    
}

最佳答案

快速修复当前代码的问题:

  1. 如果用户选择不可能的答案,请将所有分数变量设置为 0,而不仅仅是 Score2
  2. 将 do/while 循环更改为简单的 while 循环:

    while(x<NumberofQuestions)  
    {  …
    

    而不是:

    do
    { ...    
    

    另请注意,您在仅提出 2 个问题时使用了 3 个分数变量

关于java - Do-while 循环未打印正确的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58696406/

相关文章:

java - 带有 grails timeZoneSelect 标记的 IllegalAccessException on google app engine/java

javascript - angularjs - 从输入选择中删除选项

python - numpy,获取最大的子集

java - 一旦达到限制就展开数组

java - 使用数组获取正数

java - java中的字符串索引超出范围错误

java - Eclipse 打不开

java - 尝试保存凭据时,使用 Google Drive API 库和 Play with Java 会卡住/锁定应用程序

java - 如何将Oracle9i与JAVA或JSP集成?

c - `putchar` 的以下两种用法有什么区别?