确保用户输入在多个输入上加倍
在C#,控制台中,我正在提示用户输入输入,我想确保它们是双重的,如果没有,则会提示用户重新输入该值。
这是我的代码:
public static void userExpenses() {
Console.Write("Enter your Monthly Income (before deductions): ");
Program.Income = double.Parse(Console.ReadLine());
Console.Write("Enter your estimated monthly tax: ");
Program.Deductions[0] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 1. Groceries: ");
Program.Deductions[1] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 2. Water + Lights: ");
Program.Deductions[2] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 3. Transportation: ");
Program.Deductions[3] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 4. Phone/s: ");
Program.Deductions[4] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 5. Other: ");
Program.Deductions[5] = Int32.Parse(Console.ReadLine());
}
有办法做到这一点吗?
In C#, Console, I am prompting the user to enter inputs and I want to make sure that those are double and if not, will then prompt the user to re-enter the value.
Here's my code:
public static void userExpenses() {
Console.Write("Enter your Monthly Income (before deductions): ");
Program.Income = double.Parse(Console.ReadLine());
Console.Write("Enter your estimated monthly tax: ");
Program.Deductions[0] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 1. Groceries: ");
Program.Deductions[1] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 2. Water + Lights: ");
Program.Deductions[2] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 3. Transportation: ");
Program.Deductions[3] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 4. Phone/s: ");
Program.Deductions[4] = Int32.Parse(Console.ReadLine());
Console.Write("Enter monthly expenses for 5. Other: ");
Program.Deductions[5] = Int32.Parse(Console.ReadLine());
}
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下内容:
用法:
思维:我使用
十进制
,因为浮点类型在处理金钱时确实很糟糕。您可以选择使用double
或int
。他们也有一个tryparse
方法。You could use something like:
Usage:
Mind: I used
decimal
since floating point types are really bad when dealing with money. You can chose to usedouble
orint
, though. They, too have aTryParse
method.