c - 我的代码在计算每周工资时为每个输出返回 0
问题描述
我已经将每小时的费率(bpr)设置为 12。我的目标是在用户输入的小时数下找到总工资、税金和净工资。此外,如果小时数超过 40,则费率增加 1.5。所以额外时间的费率是 18 小时而不是 12 小时。这是我的代码:
#include <stdio.h>
int main ()
{
//program to calculate weekly pay
int hrs = 0;
int bpr = 12;
double gross_pay = 0.0;
double tax = 0.0;
double net_pay = 0.0;
printf("Enter the no of hours worked in a week: ");
scanf("%d", &hrs);
if (hrs <= 40)
gross_pay =(bpr * hrs);
else
{
gross_pay = (bpr * hrs);
double overtimepay = (hrs - 40) * (bpr * 1.5);
gross_pay += overtimepay;
}
if (gross_pay <= 300)
tax = (0.15 * gross_pay);
else if (gross_pay > 300 && gross_pay <= 450)
tax = ((0.15 * 300) + 0.2 * (gross_pay - 300));
else
tax = ((0.15 * 300) + (0.2 * 150) + 0.25 * (gross_pay - 450));
net_pay = (gross_pay - tax);
printf("The gross pay is: %d\n",gross_pay);
printf("The total tax amount is: %d\n",tax);
printf("The net pay is: %d\n",net_pay);
return 0;
}
代码每次输出都返回 0。故障在哪里?
解决方案
您通过将类型错误的数据传递给printf()
.
%d
是为了打印int
,但你是通过double
。
double
要通过打印printf()
,您可能应该使用%f
or %g
。(还有一些其他说明符可以double
以不同的格式打印)
推荐阅读
- css - 修改 Bootstrap Carousel 控件的高度?
- git - 是否有一种基于 Git 版本在 Compose 中自动标记 Docker 图像的方法?
- vuejs2 - sh:vue-cli-service:在 aws amplify for vuejs2 应用程序的构建步骤中找不到命令
- python - 以模板语言向下游遍历 Django 模型
- javascript - 点击搜索按钮后如何渲染大量的html?
- gnu - GNU ld:-z 起源?-rpath $ORIGIN/../lib?
- excel - Excel LOOKUP 功能未更新
- sharepoint - 如何将大数据文件从 blob 存储发送到 Azure Logic App 中的 Sharepoint?
- css - 当地址栏在移动设备上折叠时,css模态背景不会填充高度
- android - 我们可以在一个孩子中删除一个节点并在另一个孩子中通过一个按钮单击来更新另一个孩子吗?