我正在努力建立一个刺激所得税计算器。我想要取得的进展是:
4.如果用户已成功输入正数字,请执行步骤5。
我试图使用下面的代码,但它根本不起作用。
//Record user's income into double list "income"
double i;
//Record user's number of children into double list "kid"
double k;
//if...elif statement to calculate the toatal tax of the user
bool incomeOK = false;
bool kidOK = false;
do
{
Console.Write("What is your total income : ");
incomeOK = double.TryParse(Console.ReadLine(), out i);
if (!incomeOK || i < 0)
{
if (!incomeOK)
{
Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
}
else if (i < 0)
{
Console.WriteLine("Your income cannot be negative.");
}
}
else
{
Console.Write("How many children do you have: ");
kidOK = double.TryParse(Console.ReadLine(), out k);
while (!kidOK || k < 0)
{
Console.Write("How many children do you have: ");
kidOK = double.TryParse(Console.ReadLine(), out k);
if (kidOK && k >= 0)
{
//Calculate the total payable tax of the user
double totalTax = (i - 10000 - (k * 2000)) * 0.02;
if (totalTax <= 10000)
{
Console.WriteLine("You owe no tax.");
Console.WriteLine("\n\n Hit Enter to exit.");
Console.ReadLine();
}
else
{
Console.WriteLine("You owe a total of $ " + totalTax + " tax.");
Console.WriteLine("\n\n Hit Enter to exit.");
Console.ReadLine();
}
}
if (!kidOK || k < 0)
{
if (!kidOK)
{
Console.WriteLine("You must enter a valid number.");
}
else if (k < 0)
{
Console.WriteLine("You must enter a positive number.");
}
}
}
}
}
while (!incomeOK || !kidOK);
但输出:
你的总收入是多少:sfd 输入您的收入作为一个整数美元的数字。
按任意键继续...
您需要将其拆分为多个部分。
第一:获得正数
double i;
double k;
Console.Write("What is your total income : ");
while (!double.TryParse(Console.ReadLine(), out i) || i < 0)
{
if (i < 0)
{
Console.WriteLine("Your income cannot be negative.");
}
else
{
Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
}
}
第二:获得孩子数量
Console.Write("How many children do you have: ");
while (!double.TryParse(Console.ReadLine(), out k) || k < 0)
{
if (k < 0)
{
Console.WriteLine("You must enter a positive number.");
}
else
{
Console.WriteLine("You must enter a valid number.");
}
}
第三:计算
double totalTax = (i - 10000 - (k * 2000)) * 0.02;
if (totalTax <= 10000)
{
Console.WriteLine("You owe no tax.");
Console.WriteLine("\n\n Hit Enter to exit.");
Console.ReadLine();
}
else
{
Console.WriteLine("You owe a total of $ " + totalTax + " tax.");
Console.WriteLine("\n\n Hit Enter to exit.");
Console.ReadLine();
}
每当你输入错误时,while循环会强制你留在里面。当你离开时,你可以确定输入是正确的。
PS:我会把int
用于孩子的数量,因为没有任何意义,你会有2.3个孩子。
在您的代码中,您在成功解析时不会循环,但它是负数。
由于你的两个循环基本上是相互独立的,所以我不会将它们包装在一个循环中(除非你打算多次询问,但那只是另一个循环重复外部)。保持简单,更容易看到错误。我改成了这个:
do
{
Console.Write("What is your total income : ");
incomeOK = double.TryParse(Console.ReadLine(), out i);
if (!incomeOK)
{
Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
continue;
}
if (i < 0)
{
Console.WriteLine("Your income cannot be negative.");
incomeOK = false; // You need to manually set this to false to indicate it is not ok for it to loop.
}
} while (!incomeOk)
do
{
Console.Write("How many children do you have: ");
kidOK = double.TryParse(Console.ReadLine(), out k);
if (!kidOK)
{
Console.WriteLine("You must enter a valid number.");
continue;
}
if(k < 0)
{
Console.WriteLine("You must enter a positive number.");
kidOK = false; // Same deal as before
continue;
}
/// ... Your calculations
} while (!kidOK);
代码现在分为两个循环,即收入输入和子输入。而且,真的没有必要先检查它是否是(!incomeOK || i <0)。如果不正常,则输出消息并循环。如果它是否定的,输出您的消息,并确保它循环。否则只是继续正常。