【问题标题】:Why my code doesn't run succeed. I am starting to study Programs. Can you guy help me?为什么我的代码没有运行成功。我开始学习程序。你能帮帮我吗?
【发布时间】:2022-11-23 03:35:44
【问题描述】:

【问题讨论】:

  • 将代码粘贴为文本,而不是图像。
  • 程序员可以获得的最重要的技能之一是能够以一种让他的同行能够快速理解问题的方式报告问题。通常我们会报告三件事 (1) 重现步骤(在本例中将包括代码)(2) 预期行为和 (3) 实际行为。避免使用诸如“它不起作用”或“没有成功”之类的短语,因为这些话对其他任何人都没有任何意义。你能编辑你的帖子并提供这三样东西吗?
  • 提问方式请参考stackoverflow.com/help/how-to-ask
  • 错误到底是什么。您确实需要将您的代码作为文本发布!
  • 一个问题是您永远不会获得用户输入的肉的类型,因此 menu 的值不会改变。

标签: c#


【解决方案1】:

以下内容不是很好,但会介绍一些主题供您研究:

internal class Program
{
    private static Dictionary<int, Meal> _meals;
    private static Dictionary<int, string> _meats;
    private static int _menuChoice;
    private static int _meatChoice;
    private static int _quantity;
    private static int _exitValue;

    private static void Main()
    {
        _meals = new Dictionary<int, Meal>
            {
                { 1, new("Fried rice", 40)},
                { 2, new("Phad kra pao", 55) },
                { 3, new("Phad Khana", 57) },
                { 4, new("Phad phrik kaeng", 57) },
                { 5, new("Phad phong khari", 65) }
            };

        _exitValue = _meals.Keys.Max() + 1;

        _meats = new Dictionary<int, string>
            {
                { 1, "Pork" },
                { 2, "Fish" },
                { 3, "Chicken" },
                { 4, "Shrimp" },
                { 5, "Seafood" }
            };

        GetMenuSelection();

        while (_menuChoice != _exitValue)
        {
            GetMeatSelection();
            GetQuantity();
            DisplayBill();
            GetMenuSelection();
        }
    }

    private static void GetMenuSelection()
    {
        var result = 0;
        var invalid = true;
        while (invalid)
        {
            Console.Clear();
            DisplayMenu();
            var selection = Console.ReadLine();
            if (int.TryParse(selection, out result) && result >= 1 && result <= _exitValue)
                invalid = false;
        }

        _menuChoice = result;
    }

    private static void DisplayMenu()
    {
        var padding = _meals.Max(m => m.Value.Name.Length) + 5;

        Console.WriteLine(" Menu");
        Console.WriteLine();

        foreach (var meal in _meals)
        {
            Console.WriteLine($"{meal.Key}. {meal.Value.Name.PadRight(padding, '-')}{meal.Value.Price} B.");
        }

        Console.WriteLine($"{_exitValue}. Exit");
        Console.WriteLine();
        Console.WriteLine("What do you want to order?");
    }

    private static void GetMeatSelection()
    {
        var result = 99;
        var invalid = true;
        while (invalid)
        {
            DisplayMeats();
            var selection = Console.ReadLine();
            if (int.TryParse(selection, out result) && result is >= 1 and <= 5)
                invalid = false;
        }

        _meatChoice = result;
    }

    private static void DisplayMeats()
    {
        Console.Clear();
        Console.WriteLine($"You selected {_meals[_menuChoice].Name} {_meals[_menuChoice].Price} B.");
        Console.WriteLine("Which kind of meat?");
        foreach (var meat in _meats)
        {
            Console.WriteLine($"{meat.Key}. {meat.Value}");
        }
    }

    private static void GetQuantity()
    {
        var result = -1;
        var invalid = true;
        while (invalid)
        {
            DisplayQuantity();

            var selection = Console.ReadLine();
            if (int.TryParse(selection, out result))
                invalid = false;
        }

        _quantity = result;
    }

    private static void DisplayQuantity()
    {
        Console.Clear();
        Console.WriteLine($"You selected {_meals[_menuChoice].Name} with {_meats[_meatChoice]} at {_meals[_menuChoice].Price} B.");
        Console.WriteLine("How many?");
    }

    private static void DisplayBill()
    {
        Console.WriteLine($"The cost of {_quantity} {_meals[_menuChoice].Name} is {_quantity * _meals[_menuChoice].Price} baht.");
        Console.WriteLine("Press any key to continue.");
        _ = Console.ReadLine();
    }
}

在名为Meal.cs 的单独类文件中:

internal record Meal(string Name, int Price);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 2020-09-05
    • 2020-04-11
    相关资源
    最近更新 更多