求學霸幫忙看看吧,不知道是標點,還是括號原因無法運算啊
#include <stdio.h>
int cost(int a,int b)
{ ? if(3>=b>0){
? float result=14;}
else(5>a||a>=23)
? ? {float result=14+2.3*(b-3)*1.2;?}if(23>a>=5)
? ? {?float result=14+2.3*(b-3);?}
? ? return result;?}
int main()
{?float money=cost(9,12)+cost(18,12);
? ? printf("小明每天打車的總費用:%f",money);
? ? return 0;
}/*In function 'cost':
?error: expected ';' before '{' token
?warning: no newline at end of file這是出錯的地方我也不懂哪里錯了*/
2015-07-20
對了還有else是不能用來判斷的,他是否則的意思,應該寫成這樣:
…………
else
if(5>a||a>=23)
? ? {float result=14+2.3*(b-3)*1.2;?}
if(23>a>=5)
? ? {?float result=14+2.3*(b-3);?}
? ? return result;?}
…………
2015-07-20
你#include后面為什么要加空格呢,哪個老師教你的
2015-07-20
if多重結構只要根據邏輯順序寫或嵌套一般都沒有問題,關于float result首先你在代碼中出現了很多次,其次你在if-else語句中定義的result的作用范圍并不到return result;這里~,所以應該在函數一開始就定義這個變量。不知道解釋得清不清楚= =
2015-07-20
我知道原因了,首先要把第二個else改成if,第二個if改成else。然后把int cost,改成float,不然會變成整數型,然后輸出68.。同時在開頭階段添加float result,定義變量。來個人說明下,if多重結構,else和if順序錯會出錯嗎,還有為什么要定義float result;