課程
/后端開發
/C
/C語言入門
總是不能算出結果
2017-08-14
源自:C語言入門 5-14
正在回答
定義的sum的數據類型錯誤,然后最后的printf里應該是sumc,我算的這個 答案是69.4
夢之契約 提問者
Hello_Word_ 回復 夢之契約 提問者
#include <stdio.h>double getTaxiPrice(int hours,int distance){?? ?double totalPrice = 0.0;?? ?//定義打車費用 ?? ?double perPrice = 2.3;?? ?//定義每公里單價計費 ?? ?int startPrice = 13;?? ?//定義起步價 ?? ?if(hours<0 || hours>24){?? ??? ?printf("請填寫正確的時間\n");?? ??? ?return 0;?? ?}?? ?else if(!(hours>=5 && hours<23))?? ?//判斷打車時間是否要增加費用?? ?{?? ??? ?perPrice *= 1.2;?? ?//費用增加20%??????????????????????? ??? ?}?? ?if(distance >3)?? ??? ?//判斷公里數?? ?{?? ??? ?totalPrice = startPrice +(distance - 3)*perPrice;?? ?//計算價錢?? ?}?? ?else?? ?{?? ??? ?totalPrice = startPrice;?? ??? ?}?? ?totalPrice++;?? ?//加一塊錢的燃油費?? ?return totalPrice;}int main(){?? ?int moring = 9;?? ?//定義上午打車時間?? ?int afternoon = 18;?? ?//定義下午打車時間?? ?int distance = 12;?? ?//定義打車公里數?? ?double totalPrice = 0;?? ?//定義總費用?? ?if(getTaxiPrice(moring,distance) != 0)?? ?{?? ??? ?totalPrice = getTaxiPrice(moring,distance);?? ?//調用計算費用的函數?? ?}?? ?if(totalPrice != 0)?? ?{?? ??? ?totalPrice += getTaxiPrice(afternoon,distance);?? ?//調用計算費用的函數?? ?}?? ?printf("小明每天打車的總費用是:%.2f\n",totalPrice);?? ?//輸出?? ?return 0;?? ?}這個好理解一點
#include <stdio.h>double pri(int mile,int times){??? double sum;??? if(mile<=3)??? sum=13+ 1;??? else??? sum=2.3*(mile- 3)+ 14;??? if(times>=23||times<=5)??? sum=2.3*1.2*(mile-3)+14;??? return sum;}int main(){? double suma=pri(12,9);??? double sumb=pri(12,18);??? double sumc=suma+sumb;??? printf("小明每天打車的總費用為%lf\n",sumc);??? return 0;}
舉報
C語言入門視頻教程,帶你進入編程世界的必修課-C語言
2 回答這代碼哪里錯了!不能輸出結果
1 回答這個計算結果不對啊,是哪里錯了啊
4 回答哪里錯了,數不出結果
2 回答運行結果是sum=0,哪里出錯了?
3 回答我算的結果不對,請問我哪里寫錯了?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-08-14
定義的sum的數據類型錯誤,然后最后的printf里應該是sumc,我算的這個 答案是69.4
2017-08-14
#include <stdio.h>
double getTaxiPrice(int hours,int distance)
{
?? ?double totalPrice = 0.0;?? ?//定義打車費用
?? ?double perPrice = 2.3;?? ?//定義每公里單價計費
?? ?int startPrice = 13;?? ?//定義起步價
?? ?if(hours<0 || hours>24){
?? ??? ?printf("請填寫正確的時間\n");
?? ??? ?return 0;
?? ?}
?? ?else if(!(hours>=5 && hours<23))?? ?//判斷打車時間是否要增加費用
?? ?{
?? ??? ?perPrice *= 1.2;?? ?//費用增加20%??????????????????????? ?
?? ?}
?? ?if(distance >3)?? ??? ?//判斷公里數
?? ?{
?? ??? ?totalPrice = startPrice +(distance - 3)*perPrice;?? ?//計算價錢
?? ?}
?? ?else
?? ?{
?? ??? ?totalPrice = startPrice;?? ?
?? ?}
?? ?totalPrice++;?? ?//加一塊錢的燃油費
?? ?return totalPrice;
}
int main()
{
?? ?int moring = 9;?? ?//定義上午打車時間
?? ?int afternoon = 18;?? ?//定義下午打車時間
?? ?int distance = 12;?? ?//定義打車公里數
?? ?double totalPrice = 0;?? ?//定義總費用
?? ?if(getTaxiPrice(moring,distance) != 0)
?? ?{
?? ??? ?totalPrice = getTaxiPrice(moring,distance);?? ?//調用計算費用的函數
?? ?}
?? ?if(totalPrice != 0)
?? ?{
?? ??? ?totalPrice += getTaxiPrice(afternoon,distance);?? ?//調用計算費用的函數
?? ?}
?? ?printf("小明每天打車的總費用是:%.2f\n",totalPrice);?? ?//輸出
?? ?return 0;?? ?
}
這個好理解一點
2017-08-14
#include <stdio.h>
double pri(int mile,int times)
{
??? double sum;
??? if(mile<=3)
??? sum=13+ 1;
??? else
??? sum=2.3*(mile- 3)+ 14;
??? if(times>=23||times<=5)
??? sum=2.3*1.2*(mile-3)+14;
??? return sum;
}
int main()
{
? double suma=pri(12,9);
??? double sumb=pri(12,18);
??? double sumc=suma+sumb;
??? printf("小明每天打車的總費用為%lf\n",sumc);
??? return 0;
}