我正在开发一个计算应付所得税的模块化程序。在我看来这是有道理的,但我的程序只运行一行代码,然后就停止了……没有错误消息,什么也没有。我感觉这与我的 CalculateTaxDue 类中的 while 循环有关
程序必须: a) 从用户那里获得有效输入(在一个类中) b) 计算应纳税额(在另一个类中) c) 显示用户输入的应纳税所得额,按照统一税率缴纳应纳税额,根据可变百分比的应缴税款和应缴税款总额(在另一类中)
到目前为止,这是我的程序。我觉得它应该运行正常,但它没有。
class Program
{
static void Main(string[] args)
{
// declare variables
double taxableIncome, incomeTaxDue, flatRate, variablePercentage;
// get user input
taxableIncome = GetTaxableInput.GetDouble("Please enter your total income: ");
// calculate tax due
incomeTaxDue = CalculateTaxDue.GetTaxableIncome(taxableIncome);
// display taxable income
DisplayTaxRate.DisplayTaxableIncome("Your total income is: ", taxableIncome);
// display flat rate
flatRate = CalculateTaxDue.GetFlatRate(taxableIncome);
DisplayTaxRate.DisplayFlatRate("Your tax rate is: ", flatRate);
// display variable percentage
variablePercentage = CalculateTaxDue.GetVariablePercentage(taxableIncome);
DisplayTaxRate.DisplayVariablePercentage("Your variable percentage is: ", variablePercentage);
// display total tax due
DisplayTaxRate.DisplayTotalTaxDue("Your total income tax due is: ", incomeTaxDue);
// keep console open
Console.ReadLine();
}
}
class GetTaxableInput
{
public static double GetDouble(string message)
{
// declare variable
double result;
// ask for input
Console.Write(message);
// capture and validate the user input
while (!double.TryParse(Console.ReadLine(), out result))
{
Console.WriteLine("Invalid input ");
Console.Write(message);
};
return result;
}
}
class CalculateTaxDue
{
public static double GetTaxableIncome(double taxableIncome)
{
double incomeTaxDue;
double flatRate;
double variablePercentage;
// calculate and return total income tax due.
while (!double.TryParse(Console.ReadLine(), out incomeTaxDue))
{
if (taxableIncome < 0)
{
flatRate = 0;
variablePercentage = 0;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome < 49999)
{
variablePercentage = 0.05;
flatRate = 0;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome < 99999)
{
variablePercentage = 0.07;
flatRate = 2500;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome > 100000)
{
variablePercentage = 0.09;
flatRate = 6000;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
}
return incomeTaxDue;
}
// calculate and return the tax rate
public static double GetFlatRate(double taxableIncome)
{
double incomeTaxDue;
double flatRate;
double variablePercentage;
while (!double.TryParse(Console.ReadLine(), out flatRate))
{
if (taxableIncome < 0)
{
flatRate = 0;
variablePercentage = 0;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome < 49999)
{
variablePercentage = 0.05;
flatRate = 0;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome < 99999)
{
variablePercentage = 0.07;
flatRate = 2500;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome > 100000)
{
variablePercentage = 0.09;
flatRate = 6000;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
}
return flatRate;
}
// calculate and return the variable percentage ( tax rate )
public static double GetVariablePercentage(double taxableIncome)
{
double incomeTaxDue;
double flatRate;
double variablePercentage;
while (!double.TryParse(Console.ReadLine(), out variablePercentage))
{
if (taxableIncome < 0)
{
flatRate = 0;
variablePercentage = 0;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome < 49999)
{
variablePercentage = 0.05;
flatRate = 0;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome < 99999)
{
variablePercentage = 0.07;
flatRate = 2500;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
else if (taxableIncome > 100000)
{
variablePercentage = 0.09;
flatRate = 6000;
incomeTaxDue = taxableIncome * variablePercentage + flatRate;
}
}
return variablePercentage;
}
}
class DisplayTaxRate
{
public static void DisplayTaxableIncome(string message, double numberValue)
{
Console.WriteLine("{0} {1:c}", message, numberValue);
}
public static void DisplayFlatRate(string message, double numberValue)
{
Console.WriteLine("{0} {1:c}", message, numberValue);
}
public static void DisplayVariablePercentage(string message, double numberValue)
{
Console.WriteLine("{0} {1:P}", message, numberValue);
}
public static void DisplayTotalTaxDue(string message, double numberValue)
{
Console.WriteLine("{0} {1:c}", message, numberValue);
}
}
它在那里。就像我之前说的,我没有收到任何错误消息,所以我不知道从哪里开始。谢谢!