在 java 中存储数字猜谜游戏的高分问题

问题描述 投票:0回答:1

我在存储多个具有不同名称和分数的玩家的游戏高分时遇到问题。无论玩家得分如何,代码都会输出他们击败了高分。最大轮数为 15。问题似乎是高分变量在再次经过完整循环时重置,并且不会为新玩家保留之前的高分。整个游戏功能都在一个 do while 循环中。

String highScorer = " ";
                int highScore = 15;
                if (score < highScore) {
                    highScorer = name;
                    highScore = score;
                    System.out.println("Congrats! You beat the high score!");
                }
                //output person with high score
                System.out.println("The high score belongs to " + highScorer + " at " + highScore + " tries!");

当循环多个玩家的 do while 循环时,代码不会一致地存储高分

java loops variables while-loop nested-loops
1个回答
0
投票

再次通过完整循环时,高分变量会重置

从上面的行中,您可能已经在循环内初始化了

highScore
变量。尝试在循环外初始化,这样它就不会被重置。

© www.soinside.com 2019 - 2024. All rights reserved.