一、比較傳統的輸入方法用輸入流,得到字符串后要另行判斷、轉換
案例
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MainRun { public static void main(String[] args) { try { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(System.in)); System.out.println("請輸入一個整數:"); String str1 = bufferedReader.readLine(); Integer int1 = Integer.valueOf(str1); System.out.println("輸入的整數是:" + int1); System.out.println("請輸入一個浮點數:"); String str2 = bufferedReader.readLine(); Double double1 = Double.valueOf(str2); System.out.println("輸入的浮點數是:" + double1); } catch (IOException e) { e.printStackTrace(); } } }
請輸入一個整數:
234
輸入的整數是:234
請輸入一個浮點數:
23
輸入的浮點數是:23.0
二、JDK5以后有了Scanner處理控制台輸入
格式1: Scanner sc = new Scanner(new BufferedInputStream(System.in));
格式2: Scanner sc = new Scanner(System.in);
在有多行數據輸入的情況下,一般這樣處理:
while(sc.hasNextInt()){...} 或者 while(sc.hasNext()){}
讀入一個字符串: String str = sc.next();
讀入一整行數據: String lineString=sc.nextLine();
讀入一個布爾值: boolean boolenaNumber = sc.nextBoolean();
讀入一個字節型數據: byte byteNumbe = sc.nextByte();
讀入一個短整型數據: short shortNumber=sc.nextShort();
讀入一個整數: int intNumber = sc.nextInt();
讀入一個長整型數據: long longNumber=sc.nextLong();
讀入一個單精度浮點數: float floatNumber=sc.nextFloat();
讀入一個雙精度浮點數: double doubleNumber=sc.nextDouble();
對於計算代碼運行時間:
long startTime = System.nanoTime(); // … the code being measured … long estimatedTime = System.nanoTime() - startTime;
輸入案例:
輸入數據有多組,每組占2行,第一行為一個整數N,指示第二行包含N個實數
Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); for(int i=0;i<n;i++) { double a=sc.nextDouble(); ... }
三、輸出:
輸出內容:System.out.print("");
輸出內容並換行:System.out.println("");
格式化輸出: System.out.format(String format, Object ... args);
等價於 System.out.printf((String format, Object ... args);
各種格式化樣式說明:http://www.cnblogs.com/zhangpengshou/p/3699744.html
格式化輸出案例:
// System.out.printf(format, args);format為指定的輸出格式,args參數 System.out.printf("%+8.3f\n", 3.14); // "+"表示后面輸出的數字會有正負號,正的+,負的- // ;8.3f表示輸出浮點數,寬度為8,小數點保持3位有效 System.out.printf("%+-8.3f\n", 3.14);// "-"表示靠左對齊 System.out.printf("%08.3f\n", 3.14);// "0"8位寬度中自動補0 System.out.printf("%(8.3f\n", -3.14);// "("如果是負數,自動加上( ) System.out.printf("%,f\n", 123456.78); // ","金錢表示方法,每三位有一個逗號 System.out.printf("%x\n", 0x2a3b); // 輸出16進制數 System.out.printf("%#x\n", 0x2a3b);// 輸出帶0x標識的16進制數 System.out.printf("老板:您名字%s,年齡:%3d歲,工資:%,-7.2f\n", "ajioy", 21, 36000.00); System.out.printf("老板:您名字%1$s,年齡:%2$#x歲\n", "ajioy", 38); // "n{1}quot;表示用第n個參數
輸出結果:
+3.140
+3.140
0003.140
(3.140)
123,456.780000
2a3b
0x2a3b
老板:您名字ajioy,年齡: 21歲,工資:36,000.00
老板:您名字ajioy,年齡:0x26歲
五、規格化輸出
SimpleDateFormat:SimpleDateFormat是DateFormat的子類,用於格式化日期時間。
SimpleDateFormat myFmt = new SimpleDateFormat( "yyyy年MM月dd日 HH時mm分ss秒 E "); System.out.println(myFmt.format(new Date()));
輸出結果:
2016年09月08日 17時34分01秒 星期四
DecimalFormat:DecimalFormat 是 NumberFormat 的一個具體子類,用於格式化十進制數字。
DecimalFormat案例:
DecimalFormat df1 = new DecimalFormat("0.0"); DecimalFormat df2 = new DecimalFormat("#.#"); DecimalFormat df3 = new DecimalFormat("000.000"); DecimalFormat df4 = new DecimalFormat("###.###"); System.out.println(df1.format(12.34)); System.out.println(df2.format(12.34)); System.out.println(df3.format(12.34)); System.out.println(df4.format(12.34));
輸出結果:
12.3
12.3
012.340
12.34
注意:
在線編程類名是public class Main