【问题标题】:How to use a do while loop in this situation for C#?在这种情况下如何为 C# 使用 do while 循环?
【发布时间】:2015-01-08 03:54:56
【问题描述】:
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 而不是 small、medium 或 large,我会在哪里以及如何在这个语句中放置一个 while 循环或执行 while 循环,以便让我回到开头。

【问题讨论】:

  • 您可以将它放在您显示的整个代码块周围(开头的while(..) { 部分和结尾的结尾})-您将可以决定循环条件(何时结束循环)
  • 这可能是一个很好的问题,但如果您尝试用您的代码编写该 do/while 循环,它会工作得更好。如果它不起作用或出错,我们将能够更好地提供指导。
  • 你的业务逻辑也有错误。除了 SMALL 或 MEDIUM 之外,您应该在任何输入上默认为 LARGE。营销 101 ;-)

标签: c# loops for-loop


【解决方案1】:

我找到了答案!

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();再次,所以它可能存在断章取义!

【讨论】:

  • 我有另一个版本的答案 - 现在将分享。
【解决方案2】:

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

//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);

把它放在你的 main 方法中试试看!

【讨论】:

  • 哇!我真的很喜欢你解决这个问题的方式!或者你也可以使用 string.empty !无论哪种方式,您都从我这里获得了一个虚拟巧克力曲奇:D
  • @MattJones 如果您认为您的问题已得到解答,请单击复选标记图标。你也可以接受你自己的答案——我相信它甚至会让你第一次获得徽章。
【解决方案3】:

我认为您不需要花点时间看这里:您可以定义一个案例“默认”并再次要求输入,以防输入不是这 3 个案例中的任何一个。要定义默认情况,您可以执行以下操作:

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

【讨论】:

  • 不起作用。编译器错误:不能在此范围内声明名为“coffeechoice”的局部变量,因为它会给“咖啡选择”赋予不同的含义。这也行不通,我希望它循环回到开头以便放入又来了!
【解决方案4】:

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

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

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);

【讨论】:

  • 有什么方法可以插入一个简单的while循环而不必声明一个布尔值?我也想让它说“错误无效输入!”然后循环。此外,您已声明输入有效为假,并且用户别无选择输入他/她想要的内容,因为它始终声明为假。我想要的只是一个简单的程序,如果你不输入 3 个选项,它会说输入无效并循环回到开头。此外,编译器还会从您的代码中给我错误!而且您还在 do 中声明了咖啡选择,这意味着它在最后一行的上下文中不存在。
  • 抛出错误“当前上下文中不存在名称‘coffeechoice’”
【解决方案5】:

你的代码应该是这样的

更新:

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);

【讨论】:

  • 这不会编译
  • 我是这么说的......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多