以下是我正在处理的定义。但我无法继续前进。 Switch语句不起作用,直接显示默认的msg。 有人可以帮我吗?
允许用户从三盒水果(苹果,香蕉或樱桃)中选择一种,然后询问水果盒的数量(5,10或15)。每个水果盒的成本如下:苹果 - 0.25美元,香蕉 - 1.60美元,樱桃 - 2.50美元。如果选择了有效的水果盒,则只应向用户请求水果盒的数量。否则显示错误消息并退出应用程序。选择水果类型和数量后,显示总价(水果盒价格乘以箱数量)。如果用户未选择有效数量,则将数量设置为零并显示错误消息。
更新代码
Console.Clear();
Console.WriteLine("\t Purchasing Fruit System\n");
Console.WriteLine("Select Fruit");
Console.WriteLine("A - Apples ");
Console.WriteLine("B - Bananas ");
Console.WriteLine("C - Cherries ");
Console.Write("Enter Your Selection: ");
string menuSelection = null;
double totalPrice = double.MinValue;
int numOfBoxes = int.MinValue;
//Get Fruit Selection
menuSelection = Console.ReadLine();
//Add a switch statement to set Price
switch (menuSelection.ToLower())
{
case "a":
totalPrice = 0.25;
menuSelection = "Apples";
break;
case "b":
totalPrice = 1.60;
menuSelection = "Bananas";
break;
case "c":
totalPrice = 2.50;
menuSelection = "Cherries";
break;
default:
Console.WriteLine("Invalid Selection !");
Console.ReadLine();
return;
}
//Display Menu of Quantitites as Long as a Valid
Console.WriteLine();
Console.WriteLine("How many boxes of {0} do you want ?", menuSelection);
Console.WriteLine("1 - 5 {0}", menuSelection);
Console.WriteLine("2 - 10 {0}", menuSelection);
Console.WriteLine("3 - 15 {0}", menuSelection);
Console.Write("Enter your Selection: ");
//Get Quantity Selection
string value = Console.ReadLine();
if (!int.TryParse (value, out numOfBoxes) || numOfBoxes < 1 || numOfBoxes > 3)
{
Console.WriteLine("Invalid Selection! Quantity is Zero");
Console.WriteLine();
return;
}
//Add a switch statement to set Quantity
switch (numOfBoxes)
{
case 1:
totalPrice *= 5;
break;
case 2:
totalPrice *= 10;
break;
case 3:
totalPrice *= 15;
break;
}
//Display Total for Fruit
if (numOfBoxes <=3)
Console.WriteLine("Quantity is zero Your total is $ 0.00");
else
Console.WriteLine("Total price of {0} boxes of {1} is: {2}", numOfBoxes, menuSelection, totalPrice);
//Pause Display
Console.WriteLine("Press Any Key to Continue.............");
Console.ReadKey();
答案 0 :(得分:2)
您在switch
声明中使用了不同的变量,我认为您需要menuSelection
而不是switchSelection
switch(menuSelection)
{
...
}
答案 1 :(得分:0)
根据您的代码风格,我可以提出下一个代码。
Console.Clear();
Console.WriteLine("\t Purchasing Fruit System\n");
Console.WriteLine("Select Fruit");
Console.WriteLine("A - Apples ");
Console.WriteLine("B - Bananas ");
Console.WriteLine("C - Cherries ");
Console.Write("Enter Your Selection: ");
string menuSelection = null;
double totalPrice = double.MinValue;
int numOfBoxes = int.MinValue;
menuSelection = Console.ReadLine ();
switch (menuSelection) {
case "a":
case "A":
totalPrice = 0.25;
menuSelection = "Apples";
break;
case "b":
case "B":
totalPrice = 1.60;
menuSelection = "Bananas";
break;
case "c":
case "C":
totalPrice = 2.50;
menuSelection = "Cherries";
break;
default:
Console.WriteLine ("Invalid Selection !");
return;
}
//Display Menu of Quantitites as Long as a Valid
Console.WriteLine("How many boxes of {0} do you want ?", menuSelection);
Console.WriteLine("1 - 5 {0}", menuSelection);
Console.WriteLine("2 - 10 {0}", menuSelection);
Console.WriteLine("3 - 15 {0}", menuSelection);
Console.Write("Enter your Selection: ");
//Get Quantity Selection
string value = Console.ReadLine ();
if (!int.TryParse (value, out numOfBoxes) || numOfBoxes < 1 || numOfBoxes > 3) {
Console.WriteLine("Invalid Selection !");
numOfBoxes = 0;
}
switch (numOfBoxes) {
case 1:
totalPrice *= 5;
break;
case 2:
totalPrice *= 10;
break;
case 3:
totalPrice *= 15;
break;
}
if(numOfBoxes == 0)
Console.WriteLine("Quantity is zero Your total is $ 0.00");
else
Console.WriteLine("Total price of {0} boxes of {1} is: {2}", numOfBoxes, menuSelection, totalPrice);
Console.ReadLine();