Re: [問題] 三元運算子

看板C_and_CPP作者 (幽光)時間13年前 (2010/10/18 15:17), 編輯推噓2(200)
留言2則, 2人參與, 最新討論串2/2 (看更多)
事實上,愛我00835說得沒錯啊? 我照著做也很正常啊? 原po是不是括號括錯啊? 這邊是我的程式碼: bigdick@bigdick:~/code$ cat idiot.cpp <--這個是我的命令列,我直接複製貼上 #include <iostream> using namespace std; int main(void) { int M; cout << "請輸入一個蠢數字: "; cin >> M; cout << (M < 0? -M:M) << endl; return 0; } ************************************************************************ 然後執行他也沒異常啊? bigdick@bigdick:~/code$ g++ -o idiot idiot.cpp <--編譯OK bigdick@bigdick:~/code$ ./idiot 請輸入一個蠢數字: 31 31 bigdick@bigdick:~/code$ ./idiot 請輸入一個蠢數字: -31 31 如您所見,負號會自己轉正,原po檢查一下程式碼吧? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 118.161.188.88

10/18 15:43, , 1F
謝謝,我剛照你的方式用了空格M<0 ? -M:M 就正常了~謝謝^^
10/18 15:43, 1F

10/18 16:00, , 2F
像我懶得記 就除了+-*/%運算全都()包起來一定對 也比較可讀
10/18 16:00, 2F
文章代碼(AID): #1Ck_Hr6j (C_and_CPP)
文章代碼(AID): #1Ck_Hr6j (C_and_CPP)