【发布时间】:2016-04-20 20:17:00
【问题描述】:
计算用户输入的应税收入应缴纳的所得税。一定要包括错误检查以确保用户没有输入负数。还有一个图表
让我陷入困境的是我的状况应该是什么,我是否声明了正确的事情以及应该是什么? 我有 将所得税、应纳税额申报为浮动
调用输入
调用集
调用输出
输入
写“输入应税收入”
进项应税收入
结束输入
设置
我不知道。
输出
帮帮我
【问题讨论】:
标签: loops if-statement while-loop
计算用户输入的应税收入应缴纳的所得税。一定要包括错误检查以确保用户没有输入负数。还有一个图表
让我陷入困境的是我的状况应该是什么,我是否声明了正确的事情以及应该是什么? 我有 将所得税、应纳税额申报为浮动
调用输入
调用集
调用输出
输入
写“输入应税收入”
进项应税收入
结束输入
设置
我不知道。
输出
帮帮我
【问题讨论】:
标签: loops if-statement while-loop
我不确定您用什么语言编写此代码,但这是您用 C 语言编写的解决方案。请记住,这是使用整数,而不是浮点数。所以答案将被截断。教科书中的问题说假设整数条目,因此我这样做了。
int main()
{
printf("Please enter your income: ");
int iIncome = 0;
int iTaxAmount = 0;
char iChecking = 0;
//use a while loop with our scanf to make sure a valid number is input.
while (iChecking == 0)
{
scanf("%d", &iIncome);
if (iIncome <= 0)
{
printf("Please enter a positive, nonzero number\n");
printf("Please enter your income: ");
}
else
{
iChecking = 1;
}
}
if (iIncome < 50000)
{
iTaxAmount = iIncome * 0.05;
}
else if (iIncome < 100000 && iIncome >= 50000)
{
//set base amount
iTaxAmount = 2500;
int iTaxableOver50k = 0;
//calculate taxable amount over 50k
iTaxableOver50k = iIncome - 50000;
//add base tax to percent value
iTaxAmount = iTaxAmount + iTaxableOver50k * 0.07;
}
else if (iIncome >= 100000)
{
//set base tax amount
iTaxAmount = 6000;
int iTaxableOver100k = 0;
//calculate taxable amount over 100k
iTaxableOver100k = iIncome - 100000;
//add base tax to percent value
iTaxAmount = iTaxAmount + iTaxableOver100k*0.09;
}
printf("\nYou must pay: $%d in tax\n", iTaxAmount);
return 0;
}
这是您使用浮点计算得出最终答案的解决方案。
#include <stdio.h>
int main()
{
printf("Please enter your income: ");
int iIncome = 0;
float iTaxAmount = 0.0f;
char iChecking = 0;
//use a while loop with our scanf to make sure a valid number is input.
while (iChecking == 0)
{
scanf("%d", &iIncome);
if (iIncome <= 0)
{
printf("Please enter a positive, nonzero number\n");
printf("Please enter your income: ");
}
else
{
iChecking = 1;
}
}
if (iIncome < 50000)
{
iTaxAmount = (float)iIncome * 0.05f;
}
else if (iIncome < 100000 && iIncome >= 50000)
{
//set base amount
iTaxAmount = 2500;
int iTaxableOver50k = 0;
//calculate taxable amount over 50k
iTaxableOver50k = iIncome - 50000;
//add base tax to percent value
iTaxAmount = iTaxAmount + (float)iTaxableOver50k * 0.07f;
}
else if (iIncome >= 100000)
{
//set base tax amount
iTaxAmount = 6000;
int iTaxableOver100k = 0;
//calculate taxable amount over 100k
iTaxableOver100k = iIncome - 100000;
//add base tax to percent value
iTaxAmount = iTaxAmount + (float)iTaxableOver100k*0.09f;
}
printf("\nYou must pay: $%.2f in tax\n", iTaxAmount);
return 0;
}
【讨论】: