You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#咖啡点单程序输入no返回后总账单累加值被重置为0问题

问题根源

总金额清零和你贴出的账单询问循环本身没有直接关系,是两处代码逻辑共同导致的:

  • TotalCoffeeCost是定义在programLoop()方法内部的局部变量,生命周期和当前方法执行周期完全绑定,方法一旦执行结束,该变量存储的数值就会被销毁,下次重新进入方法时会被重新初始化为0。
  • 账单询问逻辑中,用户输入N/NO分支直接调用了return;,这行代码会立刻终止当前programLoop()方法的执行,跳回外层Main方法的循环。而Main方法的循环每次迭代都会新建一个全新的Program实例,再重新调用programLoop(),相当于启动了一次全新的点单流程,之前累计的金额自然会被清零。
    你误以为输入NO后是“回到菜单继续点单”,实际上代码逻辑是直接终止了当前点单流程,又重新开了一笔新订单。
修复方法

如果你的预期逻辑是用户输入NO暂时不出账单,就回到加购咖啡的环节,保留之前累计的订单金额,不要在NO分支直接return退出方法,把点单、加购询问、账单询问的逻辑整合到同一个循环里即可,核心修改后的programLoop()代码如下:

private void programLoop()
{
    int TotalCoffeeCost = 0;
    TotalCoffeeCost += askForCoffee();
    bool showBill = false;

    // 没确认要出账单就一直停留在当前订单流程
    while (!showBill)
    {
        // 询问是否加购咖啡
        var invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine("");
            Console.WriteLine(" Do you want have another coffee : Y or N (Yes or No) ?");
            Console.Write(" ");
            string userChoice = Console.ReadLine().ToUpper();
            if (userChoice == "Y" || userChoice == "YES")
            {
                TotalCoffeeCost += askForCoffee();
            }
            else if (userChoice == "N" || userChoice == "NO")
            {
                invalidChoice = false;
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }

        // 询问是否出示账单
        invalidChoice = true;
        while (invalidChoice)
        {
            Console.WriteLine(" ");
            Console.WriteLine(" Can i bring you the bill: y or n (yes or no)");
            Console.Write(" ");
            string billChoice = Console.ReadLine().ToUpper();
            if (billChoice == "Y" || billChoice == "YES")
            {
                invalidChoice = false;
                showBill = true;
            }
            else if (billChoice == "N" || billChoice == "NO")
            {
                invalidChoice = false;
                // 不退出方法,直接回到外层循环开头,重新询问是否加购,累计金额不会丢失
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine(" Please enter a valid choice");
            }
        }
    }

    // 后续付款、结账逻辑保持原有代码即可
    invalidChoice = true;
    while (invalidChoice)
    {
        Console.WriteLine("");
        Console.WriteLine(" Your total bill amount is = {0}$", TotalCoffeeCost);
        Console.WriteLine("");
        Console.WriteLine(" Please pay the amount by entering the amount bellow");
        Console.Write(" ");
        int EnterdAmt = int.Parse(Console.ReadLine());
        if (EnterdAmt != TotalCoffeeCost)
        {
            Console.WriteLine("");
            Console.WriteLine(" Please pay the correct amount");
        }
        else
        {
            invalidChoice = false;
        }
    }

    Console.WriteLine("");
    Console.WriteLine(" Thank for buying coffee, Hope you got a amazing experience");
}

额外优化提示

你当前代码中askForCoffee()方法直接使用int.Parse(Console.ReadLine())读取用户输入,当用户输入非数字内容时程序会直接崩溃,建议替换为int.TryParse做合法性校验。

内容的提问来源于stack exchange,提问作者user19512202

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 18:39:17