天天看點

Java學習之三元運算符-if語句

三元運算符

package basis.day02;
/**
 * 三元運算符(條件表達式)?表達式1:表達式2;(若為真輸出1,若為假輸出2)
 * 其中表達式1和表達式2的資料類型要一緻。
 * 三元運算符一定程度上可以用if-else語句表示(但是反之不行)
 * @author LENOVO
 *
 */
public class TestTer {

    public static void main(String[] args){
        /*
         * 輸出兩個數中較大的數
         */
        int m = ;
        int n = ;
        int max = (m > n)? m : n;   //條件為真,輸出第一個表達式的值;條件為假輸出第二個表達式的值
        System.out.println(max);

        /*
         * 輸出三個數中較大的數
         */
        int a = ;
        int b = ;
        int c = ;
        /*
        int max = (a > b)? (a > c)? a : c :(b > c)? b : c;
        System.out.println(max);
        */

        int max1 = (a > b)? a : b;
        int max2 = (max1 > c)? max1 : c;
        System.out.println(max2);
    }
}
           

if語句

package basis.day02;

import java.util.Scanner;

/**
 * 流程控制:順序結構(程式順序執行),分支結構(if-else,switch-case),
 * 循環結構(while,do……while,for)
 * @author LENOVO
 *
 */
public class TestIf_01 {
    /*
     * if-else語句:在程式執行過程中一定有一個語句執行,并且隻有一條語句執行
     * if(條件表達式){}else{}   if(條件表達式){}else if(條件表達式){}else{}
     * 如果各個條件之間是“互斥”關系,語句是自由的。
     * 如果各個條件之間有“包含”關系,範圍大的要放在範圍小的後邊。
     */
    public static void main(String[] args){
        /*
         *從鍵盤讀入小明的成績
         */
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入小明的期末成績:");
        int score = sc.nextInt();
        /*
         * 依據不同的條件輸出不同的結果
         */
        if(score == ){
            System.out.println("小明的獎勵是:BMW車一輛");
        }else if(score > ){
            System.out.println("小明的獎勵是:iphone6s一台");
        }else if(score >= ){
            System.out.println("小明的獎勵是:參考書一本");
        }else{
            System.out.println("繼續努力!");
        }
    }
}
           
if-else對三個數進行排序練習:

package basis.day02;

import java.util.Scanner;

/**
 * if-else語句練習
 * @author LENOVO
 *
 */
public class TestIf_02 {
    public static void main(String[] args){
        //從鍵盤接受資料
        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入三個整數:");

        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        int num3 = sc.nextInt();
        /*
         * 判斷大小:先找出最大的,再将剩下的進行比較。
         */
        if(num1 <= num2){
            if(num2 <= num3){
                System.out.println(num1 + "," + num2 + "," + num3);
            }else if (num1 <= num3){
                System.out.println(num1 + "," + num3 + "," + num2);
            }else{
                System.out.println(num3 + "," + num1 + "," + num2);
            }
        }else{
            if(num1 <= num3){
                System.out.println(num2 + "," + num1 + "," + num3);
            }else if (num2 <= num3){
                System.out.println(num2 + "," + num3 + "," + num1);
            }else{
                System.out.println(num3 + "," + num2 + "," + num1);
            }
        }
    }
}
           

if-else中(多個條件練習)

package basis.day02;

import java.util.Scanner;

/**
 * if測試題
 * @author LENOVO
 *
 */
public class TestIf_03 {

    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        System.out.println("請輸入你的身高:(cm)");
        //擷取身高
        int height = sc.nextInt();
        System.out.println("請輸入你的财富:(千萬)");
        //擷取财富值
        double fortune = sc.nextDouble();
        System.out.println("你長得帥嗎?(true/false)");
        //擷取帥值
        boolean b = sc.nextBoolean();
        /*
         * 條件篩選
         */

//      if(height > 180 || fortune > 1 || b){
//          if(height > 180 && fortune > 1 && b){
//              System.out.println("我一定要嫁給他!!!");
//          }else{
//              System.out.println("嫁吧,比上不足,比下有餘。");
//          }
//      }else{
//          System.out.println("不嫁");
//      }

        if(height >  && fortune >  && b){
            System.out.println("我一定要嫁給他!!!");
        }else if(height >  || fortune >  || b){
            System.out.println("嫁吧,比上不足,比下有餘。");
        }else{
            System.out.println("不嫁");
        }
    }
}
           

繼續閱讀