用户输入验证

时间:2018-08-20 07:51:28

标签: c#

我正在创建一个简单的所得税计算器,也想添加一些用户输入验证。目前,我正在尝试使用TryParse方法。我希望我的程序检查特定输入类型的每个输入,如果输入的输入无效,则程序将首先通知用户,然后要求他们重试。

我当前的尝试成功地检测到输入类型是否正确,但是我不确定如何重定向用户以重试。代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IncomeTaxCalculator
{
    class IncomeTaxV2
    {
        public static void Main()
        {
            // Define variables
            const double incomeTax = 0.02, deduction = 10000; // Constant values - These never change
            int children; // Amount of children
            double Taxdue, totalIncomeTax; // Decimal valued variables

            // Ask total income
            Console.Write("What is your total income: ");
            bool succeed = double.TryParse(Console.ReadLine(), out totalIncomeTax);

            // Ask amount of children
            Console.Write("How many children do you have: ");
            bool succeeded = int.TryParse(Console.ReadLine(), out children);


            // If statement to check input validation.
            if (succeed && succeeded) 
            {

                // User input validation

                // Calculate Deductions
                int childTax = children * 2000; // total amount for each child
                double total_deductions = (double)deduction + childTax; // total deductions = 10k + childtax

                // Calculate User input tax takeaway (-) the total amount of deductions (Equation above)
                double taxDueCalc = totalIncomeTax - total_deductions;

                // Find 2% of the Result for the amount of Tax due
                Taxdue = taxDueCalc * incomeTax;

                // Print result
                Console.Write("You owe a total of $" + Taxdue + " tax.");
            } else
            {
                // Notify user of error
                Console.Write("You must enter a valid number.");
                // Redirect too first set of TryParse statements

            }

            // End program
            Console.WriteLine("\n\n Hit Enter to exit.");
            Console.ReadLine();
        }
        }
}

重定向必须进入else语句。在研究了可能的方法之后,似乎我可能也学过使用函数并通过参数传递信息。

2 个答案:

答案 0 :(得分:0)

一种简单的方法是使用循环,并在用户输入完成后发出结束信号:

  bool entryCompleted = false;
  while (!entryCompleted)
  {
    if (succeed && succeeded)
    {
      // ..
      entryCompleted = true;
    }
  }

答案 1 :(得分:-1)

我将对这个问题使用递归函数。

public static void Main()
{
    incomeTax();
    // End program
    Console.WriteLine("\n\n Hit Enter to exit.");
    Console.ReadLine();
}

public void incomeTax()
{
    // Define variables
    const double incomeTax = 0.02, deduction = 10000; // Constant values - These never change
    int children; // Amount of children
    double Taxdue, totalIncomeTax; // Decimal valued variables

    // Ask total income
    Console.Write("What is your total income: ");
    bool succeed = double.TryParse(Console.ReadLine(), out totalIncomeTax);

    // Ask amount of children
    Console.Write("How many children do you have: ");
    bool succeeded = int.TryParse(Console.ReadLine(), out children);


    // If statement to check input validation.
    if (succeed && succeeded) 
    {
        // User input validation

        // Calculate Deductions
        int childTax = children * 2000; // total amount for each child
        double total_deductions = (double)deduction + childTax; // total deductions = 10k + childtax

        // Calculate User input tax takeaway (-) the total amount of deductions (Equation above)
        double taxDueCalc = totalIncomeTax - total_deductions;

        // Find 2% of the Result for the amount of Tax due
        Taxdue = taxDueCalc * incomeTax;

        // Print result
        Console.Write("You owe a total of $" + Taxdue + " tax.");
    } else {
        // Notify user of error
        Console.Write("You must enter a valid number.");
        // Redirect too first set of TryParse statements
        incomeTax();
    }
}

当输入的号码无效时,它将调用相同的功能。直到程序输入if语句。