代码之家  ›  专栏  ›  技术社区  ›  Kevin Smith

当硬币达到零时,我试图让我的wile循环停止,但它却一直在负方向运行

  •  -2
  • Kevin Smith  · 技术社区  · 4 年前
    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 那不在计划之内 while 循环,我不想使用 break .

    1 回复  |  直到 4 年前
        1
  •  2
  •   tmaj    4 年前

    你从不更新 Coins 罐子里剩下的硬币是 Coins - playersInput .

    while ((Coins - playersInput) >= 0)