返回列表 發帖

if...else if...else 判斷式

多向判斷式語法
if (條件式一) {
   程式區塊一;
}
else if(條件式二){
   程式區塊二;
}
else if(條件式三){
   程式區塊三;
}
.........
else{
   else的程式區塊;
}

[補充]
if...  只能有一個(放在第一個)
else if...  可以很多個(放在中間)
else  只能有一個(放在最後一個)


  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {   
  6.     int score; //變數名稱與要做的事有一定程度的相關
  7.     cout<<"請輸入你的成績: ";
  8.     cin>>score;
  9.     if(score==100) //在判斷兩邊的值是否相等,要用雙等號
  10.         cout<<"哇!滿分!"<<endl;
  11.     else if(score<100 && score>=60)
  12.         cout<<"恭喜你及格了,給你糖吃!"<<endl;
  13.     else if(score<60 && score>0)
  14.         cout<<"不及格!打屁股!"<<endl;
  15.     else if(score==0)
  16.         cout<<"零分?斬!"<<endl;
  17.     else
  18.         cout<<"輸入錯誤!斬!"<<endl;
  19.     system("pause");
  20.     return 0;   
  21. }
複製代碼

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見
Attention Seeker </3

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

此帖僅作者可見

TOP

返回列表