C# 我需要帮助为这个问题创建一个循环
我很难为这个问题创建循环 计算所得税时,应用程序需要累加从所有前一个税级到下一个税级的所有税款 收入的括号如表所示。 另外,您必须使用循环来解决此问题 这就是我目前所拥有的C# 我需要帮助为这个问题创建一个循环,c#,C#,我很难为这个问题创建循环 计算所得税时,应用程序需要累加从所有前一个税级到下一个税级的所有税款 收入的括号如表所示。 另外,您必须使用循环来解决此问题 这就是我目前所拥有的 int[] upper = { 9525, 38700, 82500, 157500, 200000, 500000, }; int[] rate = { 10, 12, 22, 24, 32, 35, 37 }; double tax; Write("Plea
int[] upper = { 9525, 38700, 82500, 157500, 200000, 500000, };
int[] rate = { 10, 12, 22, 24, 32, 35, 37 };
double tax;
Write("Please enter your income >> ");
double income = Convert.ToInt32(ReadLine());
for (int i = 0; i < upper.Length; i++)
{
if (income <= upper[i])
{
tax = income * rate[i];
}
}
任何帮助都将不胜感激 这个小代码有太多问题 你写的税太多了,而你想把所有税的价值加起来,形成一个所得税
您的条件收入以下是一个独立的静态函数,用于计算所需的结果。虽然这可能不会对练习产生影响,但一般来说,不建议使用浮点数进行货币计算,因为处理大量数据时可能会失去精度;可以转换为最低单位美分或其他任何单位,并使用整数或长数,或者使用小数,如下所示
public static decimal IncomeTax(decimal income)
{
// Only positive income can be taxed
if (income <= 0M) return 0M;
// Initialize bracket limits and rate percentages
int[] brackets = { 0, 9525, 38700, 82500, 157500,
200000, 500000, int.MaxValue };
int[] rates = { 10, 12, 22, 24, 32, 35, 37 };
// Start with zero tax
decimal tax = 0M;
// Loop over tax rates and corresponding brackets
for (int i = 0; i < rates.Length; i++)
{
// Calculate amount of current bracket
int currBracket = brackets[i + 1] - brackets[i];
// If income is below lower bound, exit loop
if (income < brackets[i]) break;
// If income is between lower and upper bound,
if (income < brackets[i + 1])
{
// adjust bracket length accordingly
currBracket = income - brackets[i];
}
// Add tax for current bracket to accumulated tax
tax += (rates[i] / 100M) * currBracket;
}
// Return result
return tax;
}
这看起来像学校的项目!!在这种情况下,您不需要其他人粘贴您的代码,但不要将代码作为图片包含在内。将代码添加到问题中。另外,你的问题也要具体,不要在一篇文章中提出多个问题。这只会给出最后一个税如果你想把所有的税加起来,那么每次都要加上以前的税。你的帮助肯定把我推向了正确的方向。非常感谢你!
//added , 9999999999 to make the last slab (37%) tends to infinite
long[] upper = { 9525, 38700, 82500, 157500, 200000, 500000, 9999999999 };
int[] rate = { 10, 12, 22, 24, 32, 35, 37 };
double tax = 0.0;
Console.WriteLine("Please enter your income >> ");
//if income is of double data type, we should convert console into double instead of int
double income = Convert.ToDouble(Console.ReadLine());
double taxCalculatedAmount = 0.0;
for (int i = 0; i < upper.Length; i++)
{
//Note: here I am calculating the delta
//which I will use to calculate the tax for the slab.
double incomeToCalculateTax =
upper[i] <= income ? (i == 0 ? upper[i] : upper[i] - upper[i - 1]) :
(i == 0 ? income : income - upper[i - 1]);
//Condition which will check if amount on which we have calculated tax is exceeding total amount
if (income > taxCalculatedAmount)
{
//proper formula of calculating tax
tax += (incomeToCalculateTax * rate[i]) / 100;
taxCalculatedAmount += incomeToCalculateTax;
}
else
break;
}
public static decimal IncomeTax(decimal income)
{
// Only positive income can be taxed
if (income <= 0M) return 0M;
// Initialize bracket limits and rate percentages
int[] brackets = { 0, 9525, 38700, 82500, 157500,
200000, 500000, int.MaxValue };
int[] rates = { 10, 12, 22, 24, 32, 35, 37 };
// Start with zero tax
decimal tax = 0M;
// Loop over tax rates and corresponding brackets
for (int i = 0; i < rates.Length; i++)
{
// Calculate amount of current bracket
int currBracket = brackets[i + 1] - brackets[i];
// If income is below lower bound, exit loop
if (income < brackets[i]) break;
// If income is between lower and upper bound,
if (income < brackets[i + 1])
{
// adjust bracket length accordingly
currBracket = income - brackets[i];
}
// Add tax for current bracket to accumulated tax
tax += (rates[i] / 100M) * currBracket;
}
// Return result
return tax;
}