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

如何在C#多方法程序中跟踪快餐点餐的累计总额?

如何在多方法的C#快餐点餐程序中跟踪累计总额

要解决累计总额的跟踪问题,核心是维护一个持久的总额变量,并在每次用户点餐后将当前单品的小计累加进去。下面提供两种简单可行的实现方式:


方式一:利用Main方法内的共享变量(快速改造现有代码)

你的现有代码中已经在Main方法内定义了局部变量,内部嵌套方法可以直接访问这些变量。只需添加一个累计总额变量,并修改小计计算逻辑即可:

修改后的完整代码

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool ordering = true;
            string userInput;
            double itemPrice;
            double itemQuantity;
            double total = 0; // 新增:累计总额变量
            string response;
            
            void pricing()
            {
                Console.Write("请选择1、2、3: ");
                userInput = Console.ReadLine();
                switch (userInput) 
                {
                    case "1":
                        itemPrice = 3.00;
                        Console.WriteLine("你选择了汉堡。");
                        break;
                    case "2":
                        itemPrice = 1.50;
                        Console.WriteLine("你选择了薯条。");
                        break;
                    case "3":
                        itemPrice = 1.00;
                        Console.WriteLine("你选择了汽水。");
                        break;
                    default:
                        Console.WriteLine("这不在我们的菜单上。");
                        pricing();
                        break;
                }
            }

            void quantity()
            {
                Console.Write("请输入数量: ");
                // 新增:简单的输入验证,防止非数字输入
                while (!double.TryParse(Console.ReadLine(), out itemQuantity) || itemQuantity <= 0)
                {
                    Console.WriteLine("请输入有效的正数值数量。");
                    Console.Write("请输入数量: ");
                }
            }

            void updateTotal()
            {
                double currentSubtotal = itemQuantity * itemPrice;
                total += currentSubtotal; // 将当前单品小计累加到总额
                Console.WriteLine();
                Console.WriteLine($"当前单品小计: {currentSubtotal:C}");
                Console.WriteLine($"累计总额: {total:C}");
            }
                
            while (ordering)
            {
                Console.WriteLine("欢迎点餐,菜单如下:");
                Console.WriteLine("\n1. 汉堡 ($3.00) \n2. 薯条 ($1.50) \n3. 汽水 ($1.00)");
                Console.WriteLine();
                
                pricing();
                quantity();
                updateTotal();
                
                Console.Write("还需要点其他餐品吗?Y/N: ");
                response = Console.ReadLine()?.ToUpper() ?? "N";

                if (response != "Y")
                {
                    ordering = false;
                    Console.WriteLine($"最终总额: {total:C}");
                    Console.WriteLine("祝您用餐愉快!");
                }
            }
        }
    }
}

关键修改点

  • 新增total变量用于存储累计总额,生命周期覆盖整个点餐循环
  • 修改subTotal为updateTotal,计算当前单品小计后累加到total,并同时展示当前小计和累计总额
  • 增加基础的输入验证,避免非法输入导致程序崩溃

方式二:让方法返回值(降低耦合,更易维护)

如果希望方法更独立、不依赖外部变量,可以让pricing和quantity方法直接返回对应的值,在Main方法中完成小计计算和总额累加:

修改后的完整代码

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            bool ordering = true;
            double total = 0;
            string response;
            
            // 返回选中餐品的价格
            double GetItemPrice()
            {
                Console.Write("请选择1、2、3: ");
                string input = Console.ReadLine();
                switch (input) 
                {
                    case "1":
                        Console.WriteLine("你选择了汉堡。");
                        return 3.00;
                    case "2":
                        Console.WriteLine("你选择了薯条。");
                        return 1.50;
                    case "3":
                        Console.WriteLine("你选择了汽水。");
                        return 1.00;
                    default:
                        Console.WriteLine("这不在我们的菜单上,请重新选择。");
                        return GetItemPrice();
                }
            }

            // 返回用户输入的有效数量
            int GetItemQuantity()
            {
                Console.Write("请输入数量: ");
                while (!int.TryParse(Console.ReadLine(), out int quantity) || quantity <= 0)
                {
                    Console.WriteLine("请输入有效的正整数数量。");
                    Console.Write("请输入数量: ");
                }
                return quantity;
            }
                
            while (ordering)
            {
                Console.WriteLine("欢迎点餐,菜单如下:");
                Console.WriteLine("\n1. 汉堡 ($3.00) \n2. 薯条 ($1.50) \n3. 汽水 ($1.00)");
                Console.WriteLine();
                
                double price = GetItemPrice();
                int quantity = GetItemQuantity();
                double currentSubtotal = price * quantity;
                total += currentSubtotal;
                
                Console.WriteLine();
                Console.WriteLine($"当前单品小计: {currentSubtotal:C}");
                Console.WriteLine($"累计总额: {total:C}");
                
                Console.Write("还需要点其他餐品吗?Y/N: ");
                response = Console.ReadLine()?.ToUpper() ?? "N";

                if (response != "Y")
                {
                    ordering = false;
                    Console.WriteLine($"最终总额: {total:C}");
                    Console.WriteLine("祝您用餐愉快!");
                }
            }
        }
    }
}

优势

  • 每个方法职责单一:GetItemPrice只负责获取餐品价格,GetItemQuantity只负责获取有效数量
  • 避免了方法间共享变量的耦合,代码更清晰,后续扩展(比如添加更多餐品)更方便

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 03:10:56