Java中的選擇結構

來源:互聯網
上載者:User

標籤:分享   src   技術分享   com   equal   文法   決賽   結構   import   

1.if選擇結構

  if選擇結構是根據條件判斷之後再做處理的一種文法結構

  文法:

if(條件){  代碼塊 //條件成立之後要執行的代碼,可以是一條語句,也可以是一組語句      }

if後小括弧裡的條件是一個運算式,而且運算式的值必須為ture 或 false。

1.1使用基本的if選擇結構

  

import java.util.Scanner;public void GetPrize{            Scanner input = new Scanner(System.in);  System.out.print("請輸入成績");  int score = input.nextInt();  if(socre > 98){      System.out.println("考的不錯,獎勵");             }        }

  運行結果:請輸入成績:100

       考的不錯,獎勵

1.2使用複雜條件下的if選擇結構

  常用的邏輯運算子

  

   如果Java成績大於98分,而音樂成績大於80分,則獎勵;如果Java成績等於100分,音樂成績大於70分,也獎勵

1 public class GetPrize2(){2     public static void main(String[] args){3         int javaScore = 100;4         int muicScore = 72;5         if((javaScore > 98 && muicScore > 80) || (javaScore == 100 && muicScore > 70)){    6             System.out.println("考的不錯,獎勵");7       }8   }
  }

  運行結果:考的不錯,獎勵

1.3使用if-else選擇結構

  

if(條件){  //代碼塊1  }else{  //代碼塊2  }

  if-else結構流程圖

  

  如果Java成績大於98,獎勵;否則繼續編寫代碼

 1 public class SimpieIf{ 2   public static void main(String[] args){ 3     int score = 91; 4     if(score > 91){ 5       System.out.println("考的不錯,獎勵"); 6     }else{ 7       System.out.println("繼續編寫代碼吧"); 8     } 9   }10 }

   運行結果:繼續編寫代碼吧。

1.4多重if選擇結構

  文法:

if(條件1){  //代碼塊1}else if(條件2){  //代碼塊2}else{  //代碼塊3}

  多重if選擇結構流程圖

 

  1)else if 可以有多個或者沒有,有幾個else if塊完全取決於需要

  2)else 塊最多有一個或沒有,else塊必須放在else if塊之後

  對學生的成績進行評測,成績>=80為良好,成績>=60為中等,成績<60為差

 1 public class ScoreAssess{ 2   public static void main(String[] args){ 3     int score = 70; 4     if(score >= 80){ 5       System.out.println("良好"); 6     }else if(score >= 60){ 7       System.out.println("中等"); 8     }else{ 9       System.out.println("差");10     }11   }12 }

  程式運行結果:中等

1.5嵌套if選擇結構

  文法:

if(條件1){  if(條件2){    //代碼塊1  }else{    //代碼塊2  }}else{  //代碼塊3}

  嵌套if結構流程圖

  學校舉行運動會,百米賽跑成績在10s內的有資格進入決賽,根據性別分為男子組和女子組

 1 import java.util.*; 2 public class Runing{ 3   public static void main(String[] args){ 4     Scanner input = new Scanner(System.in); 5     System.out.print("請輸入比賽成績(s):"); 6     double score = input.nextDouble(); 7     System.out.print("請輸入性別:"); 8     String gender = input.next(); 9     if(score < 10){10      if(gender.equals("男")){11         System.out.print("進入男子組決賽");12       }else if(gender.equals("女")){13         System.out.print("進入女子組決賽");14       }15     }else{16       System.out.print("淘汰");17     }18   }19 }

  運行結果:請輸入比賽成績(s):8

       請輸入性別: 男

       進入男子組決賽

  注意:1)只有當滿足外層if選擇結構的條件時,才會判斷內層if的條件

     2)else 總是與他前面最近的缺少 else 的那個 if 相配對

 

 

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.