java流程式控制制語句

來源:互聯網
上載者:User

標籤:控制   void   格式   要求   個數   oid   流程   轉換   考試   

1.1 if語句:

if語句是指如果滿足某種條件,就進行某種處理。

 

1 public class IfDemo {2     public static void main(String[] args) {3         int x = 5;4         if( x  < 10) {5             x++;6         }7         System.out.println(x);8     }9 }

運行結果:

1.2 if…else語句:

if…else語句是指如果滿足某種條件,就進行某種處理,否則就進行另一種處理。例如,要判斷一個正整數的奇偶,如果該數字能被2整除則是一個偶數,否則該數字就是一個奇數。if…else語句具體文法格式如下:

 

 1 public class IfElseDemo{ 2     public static void main(String[] args){ 3          int i = 16 ; 4          //判斷變數,是奇偶數, 除以2,看餘數 0 1 5          if( i % 2 == 0 ){ 6              System.out.println(i+" 是偶數"); 7          }else{ 8              System.out.println(i+" 是奇數"); 9          }10     }11 }

 

運行結果:

 

1.3 if…else if…else語句:

if…else if…else語句用於對多個條件進行判斷,進行多種不同的處理。例如,對一個學生的考試成績進行等級的劃分,如果分數大於80分等級為優,否則,如果分數大於70分等級為良,否則,如果分數大於60分等級為中,否則,等級為差。if…else if…else語句具體文法格式如下:

 

 1 /* 2    if  else if else 語句 3    適合在程式中,實現多條件的判斷 4    編寫格式: 5      if(條件){ 6          if 執行體 7      }else if(條件){ 8          if 執行體 9      }else if(條件){10          if 執行體11      }else{12          else的執行體13      }14      當if中的條件是true,執行if執行體15      if中的條件是false,執行else執行體16      一個語句中,包含多個 if , 只要有1個if的條件是true,其他的代碼,不在執行17 */18 public class IfElseIfDemo{19     public static void main(String[] args){20         //成績判斷要求 ,成績>80  成績>70  成績>60  不及格21         //定義變數,儲存成績22         int grade = 75;23         //使用if else if 語句對成績判斷24         if( grade > 80 ){25             System.out.println(grade+" 成績是優");26         }else if ( grade > 70){27             System.out.println(grade+" 成績是良");28         }else if ( grade > 60){29             System.out.println(grade+" 成績是中");30         }else{31             System.out.println(grade+" 成績是差");32         }33     }34 }

 

運行結果:

 

1.4 選擇結構if語句與三元運算轉換

三元運算子,它和if-else語句類似,文法如下:

1 判斷條件 ? 運算式1 : 運算式2

三元運算子會得到一個結果,通常用於對某個變數進行賦值,當判斷條件成立時,運算結果為運算式1的值,否則結果為運算式2的值

 1 /* 2    if語句和三元運算子的替換 3    要求: 已經知道兩個數,計算最大值 4    兩個整數,比較大小 5     6    使用if還是三元 7      判斷條件多,使用if 8      三元,必須有結果的, if 可以沒有結果的 9 */10 public class IfElseDemo_1{11     public static void main(String[] args){12         int i = 15;13         int j = 6;14         //使用if語句,判斷出最大值15         if(i>j){16             System.out.println(i+" 是最大值");17         }else{18             System.out.println(j+" 是最大值");19         }20         21         //使用三元運算實現22         int k = i>j ? i : j;23         System.out.println(k+" 是最大值");24     }25 }

運行結果:

 

java流程式控制制語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.