1.1.1 鍵盤錄入兩個數據比較是否相等
鍵盤錄入兩個數據,比較這兩個數據是否相等
1.1.2 代碼案例十六
package com.itheima; import java.util.Scanner; /* * 鍵盤錄入兩個數據,比較這兩個數據是否相等 */ public class ScannerTest2 { public staticvoid main(String[] args) { // 創建對象 Scannersc = new Scanner(System.in); // 接收數據 System.out.println("請輸入第一個數據:"); int a = sc.nextInt(); System.out.println("請輸入第二個數據:"); int b = sc.nextInt(); // 比較兩個數據是否相等 // boolean flag = ((a == b) ? true : false); boolean flag= (a == b); System.out.println("flag:"+ flag); } }
1.1.3 鍵盤錄入三個數據獲取最大值
鍵盤錄入三個數據,獲取這三個數據中的最大值
1.1.4 代碼案例十七
package com.itheima; import java.util.Scanner; /* * 鍵盤錄入三個數據,獲取這三個數據中的最大值 */ public class ScannerTest3 { public staticvoid main(String[] args) { // 創建對象 Scannersc = newScanner(System.in); // 接收數據 System.out.println("請輸入第一個數據:"); int a = sc.nextInt(); System.out.println("請輸入第二個數據:"); int b = sc.nextInt(); System.out.println("請輸入第三個數據:"); int c = sc.nextInt(); // 如何獲取三個數據的最大值 int temp =(a > b? a : b); int max = (temp > c ? temp : c); System.out.println("max:"+ max); } }