尝试使用 C# 构建一个简单的咖啡订购应用程序 - 不添加咖啡的总成本

Trying to build a simple coffee-ordering app using C# - Not adding up Total Cost of Coffees

我正在尝试使用 C# 制作一个简单的咖啡应用程序。但是,它并没有在最后加起来咖啡的总价。下面是我的代码:

using System;

namespace Coffee_Program
{
    class Coffee
    {
        static void Main(string[] args)
        {
            Start:

            int TotalCoffeePrice = 0;

            System.Console.WriteLine("Our coffee shop sells 3 sizes of coffee");
            System.Console.WriteLine("1 is Small, 2 is Medium and 3 is Large");
            System.Console.WriteLine("Enter a number below corresponding to the size of the coffee:");
            int UserSelection = int.Parse(Console.ReadLine());
             
            switch (UserSelection)
            {
                case 1:
                    System.Console.WriteLine("You chose the Small Coffee.");
                    System.Console.WriteLine($"It costs 1 dollar");
                    TotalCoffeePrice += 1;
                    break;
                case 2:
                    System.Console.WriteLine("You chose the Medium Coffee");
                    System.Console.WriteLine($"It costs 2 dollars");
                    TotalCoffeePrice += 2;
                    break;
                
                case 3:
                    System.Console.WriteLine("You chose the Large Coffee");
                    System.Console.WriteLine($"It costs 3 dollars");
                    TotalCoffeePrice += 3;
                    break;

                default:
                    System.Console.WriteLine("You did not choose a number between 1-3. Please choose again");
                    goto Start;
            }

        YesOrNo:

        System.Console.WriteLine("Do you want to buy another coffee? - Yes or No");
        string UserChoice = Console.ReadLine();

        switch (UserChoice.ToUpper())
        {
            case "YES":
                goto Start;
            case "NO":
                break;
            default:
                System.Console.WriteLine("You entered an invalid choice. Please enter Yes or No");
                goto YesOrNo;
        }


        System.Console.WriteLine("Thank your for ordering your coffees.");
        System.Console.WriteLine($"Your total cost was {TotalCoffeePrice} dollars");

        }
    }
}

如果我尝试输入小咖啡和大咖啡,TotalCoffeePrice returns 就是大咖啡的价格。我该怎么办。

我知道使用 goto 不是很好的做法,但我还没有学习循环。这里出了什么问题。

您应该在您的代码中只初始化 TotalCoffeePrice 一次,每次您转到 Start:

时它都会重置为零
int TotalCoffeePrice =0;  \ << intialze befor start
Start:
\... the loop body

您应该使用每种咖啡的相应价格更新 TotalCoffeePrice

 switch (UserSelection)
            {
                case 1:
                    System.Console.WriteLine("You chose the Small Coffee.");
                    System.Console.WriteLine($"It costs 3 dollars");
                    \TotalCoffeePrice += 1;
                    TotalCoffeePrice += 3; \<<<<  the correct price to small coffee
                    break;
                case 2:
                    System.Console.WriteLine("You chose the Medium Coffee");
                    System.Console.WriteLine($"It costs 5 dollars");
                    \TotalCoffeePrice += 2;
                    TotalCoffeePrice += 5;  \<<<< increase the total price by the coast of Medium coffe which is 5
                    break;
                
                case 3:
                    System.Console.WriteLine("You chose the Large Coffee");
                    System.Console.WriteLine($"It costs 7 dollars");
                    \TotalCoffeePrice += 3;
                    TotalCoffeePrice += 7;  \<<<<<<<<<<
                    break;

                default:
                    System.Console.WriteLine("You did not choose a number between 1-3. Please choose again");
                    goto Start;
            }