为什么 while 循环不停止?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不会更新
Coins
,并且罐子中剩余的硬币数量是Coins -playersInput
。You never update
Coins
and the number of coins left in the jar isCoins - playersInput
.