學霸看看有什么問題?
#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("請輸入正確時間\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\n",taxiprice);
? ? ? ?return 0;
? ?}
}
2017-08-04
1. main函數結尾多了一個括號,price函數結尾少了一個括號,這導致這段代碼編譯時就出錯。2. 應該在else if結束前加一個return taxiprice,或者在函數price結尾加return,源代碼輸出價格為0就是因為函數沒有正確返回值。