這是代碼:
#include<stdio.h>
int main()
{
float bonus;
float profit;
int n;
printf("請輸入當前利潤(單位:萬元): ");
scanf("%f",&profit);
n=profit/10;
if(n>10){
n=11;
}
switch(n){
case 0:bonus=profit*0.1;printf("發放的獎金數為: %f萬元\n",bonus);break;
case 1:bonus=profit*0.12;printf("發放的獎金數為: %f萬元\n",bonus);break;
case 2:
case 3:bonus=profit*0.14;printf("發放的獎金數為: %f萬元\n",bonus);break;
case 4:
case 5:bonus=profit*0.16;printf("發放的獎金數為: %f萬元\n",bonus);break;
case 6:
case 7:
case 8:
case 9:bonus=profit*0.18;printf("發放的獎金數為: %f萬元\n",bonus);break;
case 10:
case 11:bonus=profit*0.2;printf("發放的獎金數為: %f萬元\n",bonus);
}
return 0;
}
然后,如果輸入96,運行截圖:

嗯。。最后一位多了一個1。。。

求講解

uj5u.com熱心網友回復:
浮點數表示問題,是近似值。uj5u.com熱心網友回復:
printf("發放的獎金數為: %f萬元\n",bonus); => printf("發放的獎金數為: %.2f萬元\n",bonus);轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/226544.html
標籤:C語言
