看板 C_and_CPP 關於我們 聯絡資訊
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) DEV C++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): switch的使用 預期的正確結果(Expected Output): 在輸入的範圍內應得到下列結果: n<5.0 : no damage 5.0<=n<5.5 :some damage 5.5<=n<6.5 : heavy damage 6.5<=n<7.5 : disaster 7.5<n heavy : disaster 錯誤結果(Wrong Output): 第一個程式碼:只有最後的default有結果顯示 第二個程式碼:expected primary-expression before "int" 程式碼(Code):(請善用置底文網頁, 記得排版) (第一個程式碼) #include <stdio.h> #include <stdlib.h> int main(void) { int n; printf("please input earthquake size: "); scanf("%d" , &n); switch(n){ case 'n<5.0': n<5; printf("no damage\n"); break; case '5.0<=n<5.5': n<5.5; printf("some damage\n"); break; case '5.5<=n<6.5': n<6.5; printf("heavy damage\n"); break; case '6.5<=n<7.5': n<7.5; printf("disaster\n"); break; default: printf("heavy disaster"); } printf("\nearthquake size level is %d\n" , n); system("PAUSE"); return EXIT_SUCCESS; } (第二個程式碼) #include <stdio.h> #include <stdlib.h> int main(void) { char n; int "a"; int "b"; int "c"; int "d"; int "e"; if(n<5) n=a; else if(5<=n<5.5) n=b; else if(5.5<=n<6.5) n=c; else if(6.5<=n<7.5) n=d; else if(7.5<n) n=e; printf("please input earthquake size: "); scanf("%d" , &n); switch(n){ case 'a': printf("no damage\n"); break; case 'b': printf("some damage\n"); break; case 'c': printf("heavy damage\n"); break; case 'd': printf("disaster\n"); break; default: printf("heavy disaster"); break; } printf("\n earthquake size level is %d\n" , n); system("PAUSE"); return EXIT_SUCCESS; } 補充說明(Supplement): 小弟是初學者,這是書上指定說能用switch最好(東華出版的C語言詳論), 有和同學討論說覺得應該用if比較好做,但怕老師說沒有照書上要求被打回票, 所以到板上來向板友請教,請板友不吝指教,有任何需要新增或改法均請板有賜教。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.66.50.41
stupid0319:case 'n<5.0' 這是新的語法嗎?? 04/03 10:07
stupid0319:int "a"; 這個是?? 04/03 10:08
有假設n<5.0時n=a
stupid0319:看起來原po是老師教到哪學到哪的類型 04/03 10:20
stupid0319:這題要用float跟if來做就可以了 04/03 10:21
我也想說要用if作但書上有先說要用switch... 還是就直接用if做不要理書本了XD
dos792:如果書上的code真的如此,議你把書燒了換一本 04/03 10:24
這是我自己打的,不是書本的,書本哪有那麼好心習題還附解答
stupid0319:switch做不是不可以,但原po看起來不太會用switch 04/03 10:25
的確是不太會用,不過老師有說要用switch,所以就打出這種程式碼... stupid0319你能給我一些指教嗎? ※ 編輯: peterkot 來自: 210.66.50.41 (04/03 10:32)
stupid0319:你把書燒就再買一本來看就會了 04/03 10:38
stupid0319:順便把老師換掉 04/03 10:41
有這麼簡單就好了...,我要用管道跟書商拿參考解答來看好了... ※ 編輯: peterkot 來自: 210.66.50.41 (04/03 10:53)
ericinttu:先去確定switch case的語法吧 下一次就給噓了. 04/03 10:53
uranusjr:話說回來第一個會有一堆 warnings 吧... 04/03 10:54
jason12308:C++不用拿ANS吧…… 04/03 11:05
drumstick:if(5<=n<5.5) ==> if(n>=5 && n<5.5) 04/03 11:37
drumstick:case XXX => XXX 是沒辦法像if那樣做判斷 04/03 12:00
drmit:老師&原po都是天才>.^~ 04/03 13:36
purpose:先換本正確又詳細的書吧,不然以後你就會後悔學了一堆 04/03 13:42
purpose:半吊子的東西 04/03 13:43
sunneo:常有人寫這樣的switch,也許有別的程式語言會支援這語法--- 04/03 15:04
sunneo:你要是改不掉還不看書就別學c了 = = 04/03 15:05
james732:http://pastie.org/1749831 這算是一種辦法...XD 04/03 15:16
james732:至於你第二個程式碼太誇張了,我也不知道該怎麼改 04/03 15:17
peacedove:第二個例子可以設另外一個新變數 或者是string 04/03 15:39
peacedove:來做原po的case裡做的事情 04/03 15:40
kevingwn:hint: switch ((n + 0.5) / 5) 04/03 16:14
kevingwn:看錯,應該是if(n<5) ... else switch(int(n+0.5)) {...} 04/03 16:26