Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在这种情况下,如何为C#使用do while循环?_C#_Loops_For Loop - Fatal编程技术网

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

在这种情况下,如何为C#使用do while循环?,c#,loops,for-loop,C#,Loops,For Loop,是的,我知道代码写得不好等等。我知道有更好的方法可以做到这一点,但我确实是一个彻头彻尾的初学者。我想知道的是,如果我放了一个无效的语句,比如我放了HJFRJKLHRKLKEJS,而不是小的、中的或大的,我会在哪里以及如何在这个语句中放一个while循环或do while循环,以使我回到开头。我想你不需要花时间看看这里:你可以定义一个“Default”案例如果输入不是这三种情况中的任何一种,请再次请求输入。要定义默认情况,可以执行以下操作: static void Main() { Conso

是的,我知道代码写得不好等等。我知道有更好的方法可以做到这一点,但我确实是一个彻头彻尾的初学者。我想知道的是,如果我放了一个无效的语句,比如我放了HJFRJKLHRKLKEJS,而不是小的、中的或大的,我会在哪里以及如何在这个语句中放一个while循环或do while循环,以使我回到开头。

我想你不需要花时间看看这里:你可以定义一个“Default”案例如果输入不是这三种情况中的任何一种,请再次请求输入。要定义默认情况,可以执行以下操作:

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

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

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

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

你的代码应该是这样的

更新:

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

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

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

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

}

把这个放在你的主要方法里,试试看

您可以将它放在所显示的整个代码块周围(开头是
while(…){
部分,结尾是
}
),您必须确定循环条件(何时结束循环)。这可能是一个好问题,但如果您尝试用代码编写do/while循环,效果会更好。如果它不起作用,或者它出错了,那么我们将处于更好的位置来提供指导。您的业务逻辑中也有一个错误。除了小型或中型输入之外,任何输入都应默认为大型。营销101;-)不起作用。编译器错误:无法在此范围内声明名为“coffeechoice”的局部变量,因为它将赋予“coffeechoice”不同的含义“.而且那也不行,我想让它回到开头,然后再放进去!有没有什么方法可以插入一个简单的while循环而不必声明布尔值?我还希望它说“错误无效输入!”然后循环。此外,您还将输入有效声明为false,用户没有选择输入他/她想要的内容,因为它总是声明为false。我想要的只是一个简单的程序,如果你不输入3个选项,它会显示无效输入,并循环回到开始。另外,编译器会给我你的代码中的错误!而且您已经在do中声明了coffeechoice,这意味着它不存在于最后一行的上下文中。抛出错误“名称'coffeechoice'不存在于当前上下文中”这不是我所说的。。。。我只是给你一个想法,如果你不能编译,告诉我我会为你做:)我有另一个版本的答案-现在就分享。哇!我也很喜欢你解决这个问题的方式!或者你也可以使用string.empty!无论哪种方式,您都从我这里获得了一个虚拟巧克力饼干:D@MattJones如果您觉得问题得到了回答,请单击复选标记图标。你也可以接受自己的答案——我相信它甚至会给你第一次颁发徽章。
//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);