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循环放入此语句中以使我回到开头。
答案 0 :(得分:1)
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();再次,所以它可能脱离背景存在!
答案 1 :(得分:1)
你已经回答了自己的问题,但这是另一种方式。您已经注意到需要修复变量范围。这是你的另一个终止条件。我从零开始使用匹配价格,只在收到有效输入时设定价格。
//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方法中并尝试一下!
答案 2 :(得分:-1)
我认为你不需要在这里看一下:你可以定义一个案例"默认"如果输入不是这3个案例中的任何一个,请再次请求输入。要定义默认大小写,您可以执行以下操作:
default:
Console.WriteLine("Please Input the type of coffee you want! Your options are small, medium, or large!");
string coffeechoice = Console.ReadLine();
break;
答案 3 :(得分:-1)
您可以使用一个变量来指示输入是否有效(如下所示)。虽然可以检查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);
答案 4 :(得分:-1)
您的代码就像这样
<强>更新:强>
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);