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

C#控制台咖啡店程序优化求助:重复代码与循环实现难题

优化你的C#咖啡店控制台程序

嘿,看到你在动手做这个咖啡店控制台项目真的很棒!你的代码已经有了核心的点单逻辑,但确实可以通过几个小技巧解决重复代码的问题,还能轻松加上循环功能让用户可以多次点单。咱们一步步来优化:

首先解决重复代码的问题

你的代码里重复出现了多次订单确认的输出,还有多个尺寸判断的逻辑,这些都可以简化:

1. 用字符串插值替代字符串拼接

原代码里的"Here is your " + Option3 + " " + Option1 + " with " + Option2 + "."可以改成更简洁的字符串插值:

Console.WriteLine($"Here is your {Option3} {Option1} with {Option2}.");

这样可读性更高,也不容易出错。

2. 提取重复逻辑为方法

把重复的订单确认、清屏这些操作封装成单独的方法,比如:

static void DisplayConfirmation(string drink, string addOn, string size)
{
    Console.Clear();
    Console.WriteLine($"Here is your {size} {drink} with {addOn}.");
    Console.WriteLine("Press any key to continue...");
    Console.ReadKey();
}

之后每次需要确认订单时,直接调用这个方法就行,不用重复写一堆代码。

3. 使用常量定义可选值

原代码里的Tea、Milk、Large这些字符串最好定义成常量,避免硬编码,也能减少拼写错误:

const string DrinkTea = "tea";
const string AddOnMilk = "milk";
const string AddOnSugar = "sugar";
const string AddOnBoth = "both";
const string AddOnNone = "none";
const string SizeSmall = "small";
const string SizeMedium = "medium";
const string SizeLarge = "large";

加上循环功能,支持多次点单

用do-while循环可以轻松实现让用户选择是否继续点单的功能,循环里包裹整个点单逻辑,最后询问用户是否要再来一份。

完整优化后的代码

using System;

class CoffeeShopProgram
{
    // 定义常量,避免硬编码
    const string DrinkTea = "tea";
    const string AddOnMilk = "milk";
    const string AddOnSugar = "sugar";
    const string AddOnBoth = "both";
    const string AddOnNone = "none";
    const string SizeSmall = "small";
    const string SizeMedium = "medium";
    const string SizeLarge = "large";

    static void Main(string[] args)
    {
        bool continueOrdering;
        do
        {
            // 第一步:选择饮品
            Console.WriteLine("Welcome to our coffee shop! What would you like to drink? (tea)");
            string selectedDrink = Console.ReadLine().ToLower(); // 转小写,避免大小写问题

            if (selectedDrink == DrinkTea)
            {
                Console.Clear();
                Console.WriteLine($"So you want a {selectedDrink}.");

                // 第二步:选择添加物
                Console.WriteLine("Do you want milk, sugar, both or none?");
                string selectedAddOn = Console.ReadLine().ToLower();

                // 验证添加物是否合法
                if (IsValidAddOn(selectedAddOn))
                {
                    Console.Clear();
                    Console.WriteLine($"So you want a {selectedDrink} with {selectedAddOn}.");

                    // 第三步:选择尺寸
                    Console.WriteLine("And what size will that be, small, medium, or large?");
                    string selectedSize = Console.ReadLine().ToLower();

                    // 验证尺寸并输出确认
                    if (IsValidSize(selectedSize))
                    {
                        DisplayConfirmation(selectedDrink, selectedAddOn, selectedSize);
                    }
                    else
                    {
                        Console.WriteLine("Sorry, that's not one of our sizes.");
                        Console.ReadKey();
                    }
                }
                else
                {
                    Console.WriteLine("Sorry, that's not a valid option.");
                    Console.ReadKey();
                }
            }
            else
            {
                Console.WriteLine("Sorry, we only serve tea right now!");
                Console.ReadKey();
            }

            // 询问是否继续点单
            Console.Clear();
            Console.WriteLine("Would you like to order something else? (yes/no)");
            string continueChoice = Console.ReadLine().ToLower();
            continueOrdering = continueChoice == "yes";

        } while (continueOrdering);

        Console.WriteLine("Thanks for visiting! Have a great day!");
    }

    // 验证添加物是否合法
    static bool IsValidAddOn(string addOn)
    {
        return addOn == AddOnMilk || addOn == AddOnSugar || addOn == AddOnBoth || addOn == AddOnNone;
    }

    // 验证尺寸是否合法
    static bool IsValidSize(string size)
    {
        return size == SizeSmall || size == SizeMedium || size == SizeLarge;
    }

    // 显示订单确认
    static void DisplayConfirmation(string drink, string addOn, string size)
    {
        Console.Clear();
        Console.WriteLine($"Here is your {size} {drink} with {addOn}.");
        Console.WriteLine("Enjoy your drink!");
        Console.ReadKey();
    }
}

优化点说明

  • 常量定义:把所有可选值定义成常量,不仅避免硬编码,还能让代码更易维护(比如要改饮品名称,只需要改常量值)。
  • 方法提取:把验证和确认逻辑拆成单独的方法,让主逻辑更清晰,也避免重复代码。
  • 大小写处理:用ToLower()把用户输入转成小写,这样不管用户输入"Tea"还是"TEA"都能识别,提升用户体验。
  • 循环功能:do-while循环确保用户至少点一次单,之后可以选择继续或退出。

这样你的代码不仅更简洁,也更易扩展——比如以后要加咖啡选项,只需要加个常量,再修改主逻辑里的饮品判断就行!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 11:17:48