确保用户输入在多个输入上加倍

发布于 2025-01-28 05:29:31 字数 1096 浏览 2 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈独秀 2025-02-04 05:29:31

您可以使用以下内容:

public static decimal GetValueFromUser(string query)
{
    Console.Write(query);
    while(!decimal.TryParse(Console.ReadLine(), out decimal result))
    {
        Console.Write("Error: Input was not a number. Try again:")
    }
    return result;
}

用法:

Program.Income = GetValueFromUser("Enter your Monthly Income (before deductions): ");

思维:我使用十进制,因为浮点类型在处理金钱时确实很糟糕。您可以选择使用doubleint。他们也有一个tryparse方法。

You could use something like:

public static decimal GetValueFromUser(string query)
{
    Console.Write(query);
    while(!decimal.TryParse(Console.ReadLine(), out decimal result))
    {
        Console.Write("Error: Input was not a number. Try again:")
    }
    return result;
}

Usage:

Program.Income = GetValueFromUser("Enter your Monthly Income (before deductions): ");

Mind: I used decimal since floating point types are really bad when dealing with money. You can chose to use double or int, though. They, too have a TryParse method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文