我们目前正在学习类中的函数,参数和参数,所以我希望保持相同的格式,因为十进制价格'和'十进制数'将被输入函数定义
我尝试了不同的方法来改变小数'进入一个'字符串'能够阻止用户输入字符而不是小数,但我不知道为什么它不起作用
因为' amount'是decimal
,需要将其转换为string
才能运行(amount == string.Empty)
,但作为第一个问题,我无法弄明白。
decimal price;
decimal amount;
Console.Write("What is the price?");
price = decimal.Parse(Console.ReadLine());
double pricenumber;
while (!double.TryParse(price, out pricenumber)) //error here
{
Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
price = decimal.Parse(Console.ReadLine());
}
Console.Write("How many were you planning on purchasing?");
amount = decimal.Parse(Console.ReadLine());
while (amount == string.Empty) //error here
{
Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
amount = decimal.Parse(Console.ReadLine());
}
答案 0 :(得分:2)
您的代码中存在一些逻辑缺陷,您必须修复这些缺陷。请参阅评论:
Console.Write("What is the price?");
price = decimal.Parse(Console.ReadLine()); // you're already parsing the user-input into
// a decimal. This is somewhat problematic, because if the user enters "foo" instead
// of "123" the attempt to parse the input will fail
double pricenumber;
while (!double.TryParse(price, out pricenumber)) // the variable "price" already contains
// a parsed decimal. That's what you did some lines above. "TryParse" expects a string to
// be parsed whereas you're committing the parsed decimal
{
Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
price = decimal.Parse(Console.ReadLine());
}
所以你应该做的是将用户输入保持为字符串,直到你再尝试解析它为止:
Console.Write("What is the price?");
string input = Console.ReadLine(); // keep as string
double pricenumber;
while (!decimal.TryParse(input, out pricenumber))
{
Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
input = Console.ReadLine();
}
您的其他尝试也是如此。再次,请看评论:
Console.Write("How many were you planning on purchasing?");
amount = decimal.Parse(Console.ReadLine()); // you're already parsing the string into a
// decimal
while (amount == string.Empty) // as you can't compare a decimal with a string, this
// creates an error
{
Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
amount = decimal.Parse(Console.ReadLine());
}
您可以采用与上述相同的方式解决问题:
Console.Write("How many were you planning on purchasing?");
input = Console.ReadLine(); // again, keep as string
while (!decimal.TryParse(input, out amount))
{
Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
input = Console.ReadLine();
}
如果有进一步优化的空间,您可以并且应该将该逻辑放入单独的方法中,因为代码几乎相同并且会导致重复。
private static decimal GetParsedInput(string question, string noticeOnFailed)
{
Console.Write(question);
input = Console.ReadLine();
decimal result;
while (!decimal.TryParse(input, out result))
{
Console.WriteLine(questionOnFailed);
input = Console.ReadLine();
}
return result;
}
用法:
decimal price = GetParsedInput(
"What is the price?",
"You've not entered a price.\r\nPlease enter a price:");
decimal amount = GetParsedInput(
"How many were you planning on purchasing?",
"You cannot leave this blank.\r\nPlease enter how many are needed:");
答案 1 :(得分:0)
试试这个:
string price;
string amount;
Console.Write("What is the price?");
price = Console.ReadLine();
double pricenumber;
while (!double.TryParse(price, out pricenumber))
{
Console.WriteLine("You've not entered a price.\r\nPlease enter a price");
price = Console.ReadLine();
}
Console.Write("How many were you planning on purchasing?");
double amountnumber;
amount = Console.ReadLine();
while (!double.TryParse(amount, out amountnumber))
{
Console.WriteLine("You cannot leave this blank.\r\nPlease enter how many are needed:");
amount = Console.ReadLine();
}
答案 2 :(得分:0)
我想通了
我已经完成了这项工作
{ public static decimal questions(string question)
{
Console.Write(question);
string answer = Console.ReadLine();
decimal result;
while (!decimal.TryParse(answer, out result))
{
Console.WriteLine(question);
answer = Console.ReadLine();
}
return result;
}
用法:
string productcost = "How much does one " + productname + " cost? ";
questions(productcost);
完美地工作