存储随机数和分数

约瑟夫·托格

我刚开始编程几天。我想向用户提出不同的问题,如果他们输入正确的问题,那么我会给他们 5 分的分数来回答正确的问题,加上使用 Random 给出的奖励分数,然后添加这个奖励(随机)到总分(score(5)+随机),该总分被存储并用于添加以下问题的下一个分数,并且该过程对所有问题重复[5]。这是我到目前为止所做的,但它不断为每个问题打印相同的结果,我想继续添加到以前的分数。

        for (int attempts = 1; attempts <= 3; attempts++);
    {   
        Random dice = new Random(); 
        for(int n = 0; n <QArray.length; n++)
        {

        System.out.println("Question" + (n+1));
        System.out.println(QArray[n]);

            for(int m =0; m<3; m++)
            {
            String ans = scanner.nextLine();    

            int t = dice.nextInt(9) + 1;
            int scoremarks = 5;  
              if (ans.equalsIgnoreCase(AArray[n]))
              {
              System.out.println("That is correct!\nYour score is:" + scoremarks + "\nWith virtual dice your total score is:" + (scoremarks +t));

              break;
              }
              else 
              {
              System.out.println("That is incorrect!\nYou got 0 Marks\nYour score is 0!");
              }
偏执狂

您需要在循环外保持分数。您可以在所有问题后打印 totalScore,并将获得所有正确答案的总分。

    int totalScore = 0;
    for (int n = 0; n < QArray.length; n++) {
        System.out.println("Question" + (n + 1));
        System.out.println(QArray[n]);

        for (int m = 0; m < 3; m++) {
            String ans = scanner.nextLine();
            Random dice = new Random();
            int t = dice.nextInt(9) + 1;
            int scoremarks = 5;
            if (ans.equalsIgnoreCase(AArray[n])) {
                totalScore += (scoremarks + t);
                System.out.println("That is correct!\nYour score is:" + scoremarks + "\nWith bonus your total score is:" + (scoremarks + t));
                // correct = false;
                break;
            } else {
                System.out.println("That is incorrect!\nYou got 0 Marks\nYour score is 0!");
            }
        }
    }

在评论中提到了一些优化:

    int totalScore = 0;
    Random dice = new Random();
    int scoremarks = 5;
    for (int n = 0; n < QArray.length; n++) {
        System.out.println("Question" + (n + 1));
        System.out.println(QArray[n]);

        for (int m = 0; m < 3; m++) {
            String ans = scanner.nextLine();
            int t = dice.nextInt(9) + 1;
            if (ans.equalsIgnoreCase(AArray[n])) {
                totalScore += (scoremarks + t);
                System.out.println("That is correct!\nYour score is:" + scoremarks + "\nWith bonus your total score is:" + (scoremarks + t));
                break;
            } else {
                System.out.println("That is incorrect!\nYou got 0 Marks\nYour score is 0!");
            }
        }
    }

您可以在答案正确时在条件中打印 totalScore 以查看每次答案正确时分数增加。同样对于答案不正确的情况,您仍然可以显示总分以查看您可能从之前的问题中获得多少分。不确定是否有 3 次尝试以获得正确的答案,但这就是 for 循环内部似乎正在做的事情,因此错误猜测的消息似乎不合适。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章