在这种情况下,如何对 C# 使用 do while 循环

本文关键字:使用 do while 循环 这种情况下 | 更新日期: 2023-09-27 18:36:37

static void Main()
{
  Console.WriteLine("Please Input the type of coffee you want! Your options are small, medium, or large!");
  double coffeeprice = 0;
  string coffeechoice = Console.ReadLine();
  switch (coffeechoice.ToUpper())
  {
    case "SMALL":
      coffeeprice += 2.00;
      break;
    case "MEDIUM":
      coffeeprice += 4.00;
      break;
    case "LARGE":
      coffeeprice += 6.00;
      break;
  }
  Console.WriteLine("Generating receipt for your {0} order now!", coffeechoice);
 }
}

是的,我知道代码写得不好等。我知道有更好的方法可以做到这一点,但我实际上是一个死去的初学者。我想知道的只是,如果我放置一个无效的语句,比如我放一个无效的语句,比如我把 HJFRJKLHRKLKEJS 而不是小、中或大,我会在哪里以及如何在这个语句中放置一个 while 循环或 while 循环。

在这种情况下,如何对 C# 使用 do while 循环

Whelp我找到了答案!

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Please Input the type of coffee you want! Your options are small, medium, or large!");
        double coffeeprice = 0;
        string coffeechoice = Console.ReadLine();
        do
        {
            switch (coffeechoice.ToUpper())
            {
                case "SMALL":
                    coffeeprice += 2.00;
                    break;
                case "MEDIUM":
                    coffeeprice += 4.00;
                    break;
                case "LARGE":
                    coffeeprice += 6.00;
                    break;
                default:
                    Console.WriteLine("Invalid input please try again!");
                    coffeechoice = Console.ReadLine();
                    break;
            }
        } while (coffeechoice.ToUpper() != "SMALL" && coffeechoice.ToUpper() != "MEDIUM" && coffeechoice.ToUpper() != "LARGE");


        Console.WriteLine("Generating receipt for your {0} order now!", coffeechoice);
    }
}

事实证明,我所要做的就是将 coffeechoice 声明为 Console.ReadLine(); 再次,这样它就可以断章取义地存在!

你已经回答了你自己的问题,但这是另一种方式。您已经注意到需要修复变量范围。这是您的另一个终止条件。我使用了与价格的匹配,因为您从零开始,并且仅在收到有效输入后设置价格。

//initialize coffeechoice to have it in scope at the end when you need it after the loop
string coffeechoice = "";
double coffeeprice = 0;
do {
    Console.WriteLine("Please Input the type of coffee you want! Your options are small, medium, or large!");
    coffeechoice = Console.ReadLine();
    switch (coffeechoice.ToUpper()) {
        case "SMALL":
            coffeeprice = 2.00;
            break;
        case "MEDIUM":
            coffeeprice = 4.00;
            break;
        case "LARGE":
            coffeeprice = 6.00;
            break;
    }
} while (coffeeprice == 0); //price not set, no valid input accepted
Console.WriteLine("Generating receipt for your {0} order now!", coffeechoice);
Console.WriteLine("You paid {0}", coffeeprice);

把它放在你的主方法中,试一试!

我认为你不需要在这里看一会儿:您可以定义一个案例"默认",并在输入不是这 3 种情况中的任何一种的情况下再次请求输入。要定义默认大小写,您可以执行以下操作:

default:
    Console.WriteLine("Please Input the type of coffee you want! Your options are small, medium, or               large!");    
string coffeechoice = Console.ReadLine();
    break;

您可以使用一个变量来指示输入是否有效(如下所示)。虽然可以检查咖啡价格 == 0,但我不喜欢这种方法,因为它没有明确说明输入,并且定价的变化可能也需要更改此逻辑。

另外,我不确定为什么您只想在输入无效时才循环,因为这会使咖啡价格 += 变得不必要。

bool isInputValid = false;
double coffeeprice = 0;
do
{
    Console.WriteLine("Please Input the type of coffee you want! Your options are small, medium, or large!");
    string coffeechoice = Console.ReadLine();
    isInputValid = true;
    switch (coffeechoice.ToUpper())
    {
        case "SMALL":
            coffeeprice += 2.00;
            break;
        case "MEDIUM":
            coffeeprice += 4.00;
            break;
        case "LARGE":
            coffeeprice += 6.00;
            break;
        default:
            isInputValid = false;
            break;
    }
}while (isInputValid == false);
Console.WriteLine("Generating receipt for your {0} order now!", coffeechoice);

你的代码是这样的

更新:

double coffeprice=0;
do
{
Console.WriteLine("Enter you choice:");
string coffeechoice = Console.ReadLine();
switch (coffeechoice.ToUpper())
    {
        case "SMALL":
            coffeeprice += 2.00;
            break;
        case "MEDIUM":
            coffeeprice += 4.00;
            break;
        case "LARGE":
            coffeeprice += 6.00;
            break;
    }
}
while(coffeprice>0);