为什么 while 循环不停止?

发布于 2025-01-17 08:04:39 字数 709 浏览 0 评论 0原文

int Coins = 27;
int playersInput =0;
int counter = 0;

while (Coins >= 0) {
            
    Console.WriteLine("There are " +  (Coins - playersInput)  + " in the bag" + "\n");
    Console.WriteLine("Turn #" + (counter + 1));
    playersInput += Convert.ToInt32(Console.ReadLine());

    if (playersInput % 2 == 0) {
       Console.WriteLine("Player 0 turn");
    } else {
       Console.WriteLine("Player 1 turn");
    }
    counter++;            
}

Console.WriteLine("The last player lost this game");

该代码必须在两个玩家之间交替,如果 coins 值达到 0,则必须打印 console.writeline 之外的 console.writeline code>while 循环,我不想使用 break

int Coins = 27;
int playersInput =0;
int counter = 0;

while (Coins >= 0) {
            
    Console.WriteLine("There are " +  (Coins - playersInput)  + " in the bag" + "\n");
    Console.WriteLine("Turn #" + (counter + 1));
    playersInput += Convert.ToInt32(Console.ReadLine());

    if (playersInput % 2 == 0) {
       Console.WriteLine("Player 0 turn");
    } else {
       Console.WriteLine("Player 1 turn");
    }
    counter++;            
}

Console.WriteLine("The last player lost this game");

The code has to alternate between two players and if the coins value reaches to 0 it has to print the console.writeline that is outside the while loop and I don't want to use break.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海未深 2025-01-24 08:04:39

您永远不会更新Coins,并且罐子中剩余的硬币数量是Coins -playersInput

while ((Coins - playersInput) >= 0)

You never update Coins and the number of coins left in the jar is Coins - playersInput.

while ((Coins - playersInput) >= 0)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文