【Java入門】資料鍵盤錄入
阿新 • • 發佈:2019-02-16
package com.itheima; import java.util.Scanner; 快捷鍵ctrl+shift+o * 鍵盤錄入:為提高程式的靈活性,把資料改為鍵盤錄入 * 實現:使用JDK提供的類Scanner * 步驟: * a.導包:import java.util.Scanner; * 在一個類中package>import>class * b.建立鍵盤錄入物件 * Scanner sc = new Scanner(System.in) * c.接受資料 * int i = sc.nestInt(); * public class KeyDemo { public static void main(String[] args) { //建立鍵盤錄入物件 System.out.println("建立鍵盤錄入物件.."); Scanner sc = new Scanner(System.in); //接收資料 System.out.println("請輸入一個數據:"); int i =sc.nextInt(); //輸出 System.out.println("i="+i); System.out.println("........Test1......"); //test 錄入兩個資料,並求和輸出結果 Scanner sc1 = new Scanner(System.in); System.out.println("第一個資料"); int a = sc1.nextInt(); System.out.println("第二個資料"); int b = sc1.nextInt(); //求和 int sum=a+b; System.out.println("sum="+sum); System.out.println("........Test2......"); Scanner sc2 = new Scanner(System.in); System.out.println("輸入第一個資料:"); int a1 = sc2.nextInt(); System.out.println("輸入第二個資料:"); int b1 = sc2.nextInt(); System.out.println("輸入第三個資料:"); int c1 = sc2.nextInt(); boolean flag = (a1==b1); //boolean flag = (a1==b1)?true:false; System.out.println("是否相等:"+flag); int temp=(a1>b1)?a1:b1; int max = (temp>c1)?temp:b1; System.out.println("max="+max); } }