当我在第一次尝试时运行它可以正常工作,但是当我输入一些垃圾时,它会在给出正确的输入后开始给我错误 “你想买另一种咖啡是或否”。 当我按是,它再次要求我输入
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Practicing
{
class Program
{
static void Main(string[] args)
{
try
{
int TotalCoffieCost = 0;
string UserDecision = string.Empty;
do
{
int UserChoice = -1;
do
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Please Enter Your Choice: 1-Small,2-Medium,3-Large");
UserChoice = int.Parse(Console.ReadLine());
switch (UserChoice)
{
case 1:
TotalCoffieCost += 50;
break;
case 2:
TotalCoffieCost += 70;
break;
case 3:
TotalCoffieCost += 100;
break;
default:
Console.WriteLine("Your choise {0} Is Invalid", UserChoice);
break;
}
} while (UserChoice != 1 && UserChoice != 2 && UserChoice != 3);
do
{
Console.WriteLine("Do You want to buy another coffie Yes or No");
UserDecision = Console.ReadLine().ToUpper();
if (UserDecision != "YES" && UserDecision != "NO") ;
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Your Choise {0} is invalid ", UserDecision);
Console.ForegroundColor = ConsoleColor.White;
}
} while (UserDecision != "YES " && UserDecision != "NO");
}
while (UserDecision.ToUpper() != "NO");
Console.WriteLine("ThankYou For shopping With Us \n Your Bill Is Genrating Please Wait");
Thread.Sleep(3000);
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Your bill {0}", TotalCoffieCost);
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
}
}
}
}
答案 0 :(得分:4)
} while (UserDecision != "YES " && UserDecision != "NO");
“YES”中有空格。
答案 1 :(得分:1)
您的代码中存在一些拼写错误。
while
语句等同于"YES "
而非"YES"
。注意额外的空间。"YES"
或"NO"
。通常也允许y
和n
。更新了代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Practicing
{
class Program
{
static void Main(string[] args)
{
try
{
int TotalCoffeeCost = 0;
string UserDecision = string.Empty;
string[] yesAnswers = new string[] { "YES", "Y" };
string[] noAnswers = new string[] { "NO", "N" };
int[] coffeeChoices = new int[] { 1, 2, 3 };
do
{
int UserChoice = -1;
do
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Please Enter Your Choice: 1-Small,2-Medium,3-Large");
UserChoice = int.Parse(Console.ReadLine());
switch (UserChoice)
{
case 1:
TotalCoffeeCost += 50;
break;
case 2:
TotalCoffeeCost += 70;
break;
case 3:
TotalCoffeeCost += 100;
break;
default:
Console.WriteLine("Your choise {0} Is Invalid", UserChoice);
break;
}
} while (!coffeeChoices.Contains(UserChoice));
do
{
Console.WriteLine("Do you want to buy another coffee Yes or No");
UserDecision = Console.ReadLine().ToUpper();
if (!yesAnswers.Contains(UserDecision) && !noAnswers.Contains(UserDecision))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Your Choise {0} is invalid ", UserDecision);
Console.ForegroundColor = ConsoleColor.White;
}
} while (!yesAnswers.Contains(UserDecision) && !noAnswers.Contains(UserDecision));
}
while (!noAnswers.Contains(UserDecision));
Console.WriteLine("Thank you For shopping with us \n Your bill is generating. Please wait...");
Thread.Sleep(3000);
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Your bill is {0}", TotalCoffeeCost);
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("(Press any key to exit)");
Console.ReadKey();
Environment.Exit(0);
}
catch (Exception e)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Message);
}
}
}
}
答案 2 :(得分:0)
这里有太多do while
个循环。例如:你的开关不需要一个 - 事实上它不应该得到一个。实际上,这整件事可能只想在一个循环中。如果他们想要购买另一个人,你想不断问一个人吗?不。您还谈到了一些您未使用的goto
。无论哪种方式 - 这是您需要的帮助:
enum SIZE
{
SMALL,
MEDIUM,
LARGE
}
static void Main(string[] args)
{
float rTotalCost = 0.0f;
string sUserDecision = string.Empty;
do
{
SIZE UserChoice;
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Please Enter Your Choice: 1-Small, 2-Medium, 3-Large\n");
UserChoice = (SIZE)int.Parse(Console.ReadLine());
switch (UserChoice)
{
case SIZE.SMALL:
rTotalCost += 2.50f;
break;
case SIZE.MEDIUM:
rTotalCost += 3.00f;
break;
case SIZE.LARGE:
rTotalCost += 3.50f;
break;
default:
Console.WriteLine("Your choise {0} Is Invalid", UserChoice);
break;
}
Console.WriteLine("\n\nDo You want to buy another coffee? Yes or No\n");
sUserDecision = Console.ReadLine().ToUpper();
Console.WriteLine("\n");
if (sUserDecision != "YES" && sUserDecision != "NO")
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n\nError! Invalid Selection Made! \n\n");
break;
}
} while (sUserDecision != "NO");
Console.WriteLine("\n\nThank you for shopping! Your total is:\t$" + rTotalCost);
Console.WriteLine("\n\nPress Any key to exit...");
}
}
不要试图这么努力。它永远不会对你有利。常见错误是过度思考您的问题,从而解决问题。另外,请随意刮开enum
同样的工作。
答案 3 :(得分:0)
您还可以通过多种方式简化用户输入的一些处理。首先,您可以围绕第一个输入创建一个硬循环,其中循环约束强制执行1.它是一个整数(使用int.TryParse
)和2.它在有效范围内(1 - 3
) :
代码如下所示,我们反复询问一个数字,直到它们给我们有效的输入。 int.TryParse
,如果成功,将使用整数填充reponse
变量,因此我们也可以在我们的条件下测试它:
int response;
do
{
Console.Write(" - Enter a number from 1 - 3: ");
} while (!int.TryParse(Console.ReadLine(), out response)
|| response < 1 || response > 3);
您可以执行的其他优化只是假设Yes|No
问题的任何输入都不是以Y
开头的No
。这可能不是你想要的,但这是一个例子,以防它适合你。基本上,你只需要输入,测试y
的第一个字符(忽略大小写),并设置一个布尔结果:
bool buyAnotherOne;
do
{
// Main code here (omitted) . . .
buyAnotherOne = Console.ReadLine().StartsWith("y", StringComparison.OrdinalIgnoreCase);
} while (buyAnotherOne);
最后一条建议是在程序结束时暂停,以便用户有机会查看总数。他们等了3秒钟,毕竟...
// (assume all other program code is above)
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();
最后,它有助于简化代码并使其更具可读性。把它们放在一起看起来像:
int totalCost = 0;
bool buyAnotherOne;
do
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("What size coffee would you like? (1-Small, 2-Medium, 3-Large)?");
int response;
do
{
Console.Write(" - Enter a number from 1 - 3: ");
} while (!int.TryParse(Console.ReadLine(), out response)
|| response < 1 || response > 3);
switch (response)
{
case 1:
totalCost += 50;
break;
case 2:
totalCost += 70;
break;
case 3:
totalCost += 100;
break;
}
Console.Write("Do You want to buy another coffee (yes or no): ");
buyAnotherOne = Console.ReadLine().StartsWith("y",
StringComparison.OrdinalIgnoreCase);
} while (buyAnotherOne);
Console.WriteLine("Thank you for shopping with us");
Console.WriteLine("Your bill is genrating; please wait...");
Thread.Sleep(TimeSpan.FromSeconds(3));
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("Your total bill: {0}", totalCost);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("\nDone!\nPress any key to exit...");
Console.ReadKey();