華文網

【java學習day03三元運算子】Test3_Scanner

/*

* A:案例演示

* 鍵盤錄入練習:鍵盤錄入兩個資料,比較這兩個資料是否相等

* B:案例演示

* 鍵盤錄入練習:鍵盤錄入三個資料,獲取這三個資料中的最大值

*/

import java.util.Scanner;//導包

class Test3_Scanner {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);//創建鍵盤錄入物件

//鍵盤錄入練習:鍵盤錄入兩個資料,比較這兩個資料是否相等

/*System.out.println("請輸入第一個整數:");

int x = sc.nextInt();//將鍵盤錄入的資料存儲在x中

System.out.println("請輸入第二個整數:");

int y = sc.nextInt();//將鍵盤錄入的資料存儲在y中

//boolean b = (x == y)? true : false;

boolean b = (x == y);

System.out.println(b);*/

//鍵盤錄入練習:鍵盤錄入三個資料,獲取這三個資料中的最大值

System.out.println("請輸入第一個整數:");

int x = sc.nextInt();//將鍵盤錄入的資料存儲在x中

System.out.println("請輸入第二個整數:");

int y = sc.nextInt();//將鍵盤錄入的資料存儲在y中

System.out.println("請輸入第三個整數:");

int z = sc.nextInt();//將鍵盤錄入的資料存儲在y中

//定義臨時變數記錄住比較出前兩個變數中的最大值

int temp = (x > y) ? x : y;

//將比較後的結果與第三個變數中的值比較,比較出三個數中的最大值

int max = (temp > z) ? temp : z;

System.out.println(max);

}

}