學(xué)霸看看有什么問(wèn)題?
#include <stdio.h>
double price(int hours,int distance)
{
? ?double taxiprice=0.0;
? ?double start=13;
? ?double everydistance=2.3;
? ?if(hours<0||hours>24)
? ?{
? ? ? ?printf("請(qǐng)輸入正確時(shí)間\n");
? ? ? ?return 0;
? ?}
? ?else if(hours>=5&&hours<23)
? ?{
? ? ? ?if(distance>3)
? ? ? ?{
? ? ? ? ? ?taxiprice=start+(distance-3)*everydistance;
? ? ? ?}
? ? ? ?else
? ? ? ?{
? ? ? ? ? ?taxiprice=start;
? ? ? ?}
? ?}
? ?else
? ?{
? ? ? ?if(distance>3)
? ? ? ?{
? ? ? ? ? ?taxiprice=start+(distance-3)*everydistance*1.2;
? ? ? ?}
? ? ? ?else
? ? ? ?{
? ? ? ? ? ?taxiprice=start;
? ? ? ?}
? ? taxiprice++;
? ? return taxiprice; ??
? ?}
? ?int main()
? ?{
? ? ? ?int moring=9;
? ? ? ?int afternoon=18;
? ? ? ?int distance=12;
? ? ? ?double taxiprice=0.0;
? ? ? ?taxiprice=price(moring,distance)+price(afternoon,distance);
? ? ? ?printf("小明每天打車總費(fèi)用:%f\n",taxiprice);
? ? ? ?return 0;
? ?}
}
2017-08-04
1. main函數(shù)結(jié)尾多了一個(gè)括號(hào),price函數(shù)結(jié)尾少了一個(gè)括號(hào),這導(dǎo)致這段代碼編譯時(shí)就出錯(cuò)。2. 應(yīng)該在else if結(jié)束前加一個(gè)return taxiprice,或者在函數(shù)price結(jié)尾加return,源代碼輸出價(jià)格為0就是因?yàn)楹瘮?shù)沒(méi)有正確返回值。