java中的條件判斷語句


一、java中的條件語句:

1、if語句:

      格式:if(判斷的條件){

                           語句1;

                  }

      解讀:當條件符合的時,程序則執行語句1.不符合條件接收程序。

      例子:package com.oldboy;

                import java.util.Scanner;

                 public class Test {

                      public static void main(String[] args) {   

                            @SuppressWarnings("resource")   

                            Scanner scanner=new Scanner(System.in);//從控制台輸入數據   

                             @SuppressWarnings("unused")   

                             int x=scanner.nextInt();//定義一個整型從變量接收從控制台傳入的數據  

                            if(x>5){    

                                System.out.println("x="+x);  

                  }

          }

}

2、if(){}else{}語句:

      格式:

             if(判斷條件){

                語句塊1;

               }else{

                語句塊2;

             }

       解讀:if(){}else{}語句是分為兩種情況,當條件成立時,執行語句塊1,當條件為假時,執行語句塊2.

       例子:

               package com.oldboy;
                import java.util.Scanner;
                  public class Test {
                       public static void main(String[] args) {
                            @SuppressWarnings("resource")
                            Scanner scanner=new Scanner(System.in);//從控制台輸入數據
                             @SuppressWarnings("unused")
                          int x=scanner.nextInt();//定義一個整型從變量接收從控制台傳入的數據
                              if(x>90){
                                  System.out.println("我的成績是優秀,我要繼續加油");
                             }else{
                                  System.out.println("我要繼續努力.......");
                             }
                       }
               }

3、if(){}else if{}else{};

      格式:if(判斷語句){

                     語句塊1;

               }else if(){

                     語句塊2;

              }

              ...{

           } else{

                    語句塊n;

            }

          解讀:這個條件判斷語句使用與多種的條件的場合。

          例如:package com.oldboy;
                       import java.util.Scanner;
                          public class Test {
                              public static void main(String[] args) {
                                   @SuppressWarnings("resource")
                                   Scanner scanner=new Scanner(System.in);//從控制台輸入數據
                                 @SuppressWarnings("unused")
                                   int x=scanner.nextInt();//定義一個整型從變量接收從控制台傳入的數據
                                       if(x>90 && x<100){
                                              System.out.println("我的成績是優秀,我要繼續加油");
                                         }else if(x>80 && x<89){
                                              System.out.println("我要繼續努力是良好");
                                         }else if(x>=60 && x<79){
                                              System.out.println("及格");
                                         }else{
                                                System.out.println("同學好好的學習");
                                            }
                            }
                 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM