C# 用户输入验证
我正在创建一个简单的所得税计算器,也希望添加一些用户输入验证。目前,我正在试验一种TryParse方法。我希望我的程序检查每个输入的具体输入类型,如果输入了无效输入,程序将首先通知用户,然后要求他们重试 我当前的尝试成功地检测到输入类型是否正确,但我不确定如何重定向用户以重试。代码:C# 用户输入验证,c#,C#,我正在创建一个简单的所得税计算器,也希望添加一些用户输入验证。目前,我正在试验一种TryParse方法。我希望我的程序检查每个输入的具体输入类型,如果输入了无效输入,程序将首先通知用户,然后要求他们重试 我当前的尝试成功地检测到输入类型是否正确,但我不确定如何重定向用户以重试。代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IncomeTaxC
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语句。在研究了可能的方法之后,我似乎也学会了使用函数并通过参数传递信息。一个简单的方法是使用循环,并在用户输入完成时发出结束信号:
bool entryCompleted = false;
while (!entryCompleted)
{
if (succeed && succeeded)
{
// ..
entryCompleted = true;
}
}
对于这个问题,我将使用递归函数
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
语句时才会出现这种情况。我被告知不要太使用循环,并调查其他选项“我被告知不要太使用循环”这是作业/家庭作业吗@Tomtok的代码结构是可变的吗?在研究了可能的方法之后,我似乎必须学会使用函数并通过参数传递信息。意味着我们不需要遵循您的结构,对吗?