一、輸入
1.使用Scanner類:
(1)使用java.util包。 import java.util.*;
(2)構造Scanner類對象,它附屬於標准輸入流System.in。 Scanner s = new Scanner(System.in);
(3)常用的next()方法系列:
nextInt():輸入整數
nextLine():輸入字符串
nextDouble():輸入雙精度數
next():輸入字符串(以空格作為分隔符)
Scanner s=new Scanner(System.in); System.out.println("輸入一個整數:"); int i = s.nextInt(); System.out.println("輸入一個雙精度浮點數:"); double d = s.nextDouble(); System.out.println("輸入一個字符串:"); String sc = s.next(); //也可以用nextLine()去掉next()支持傳入空格 s.close(); //不關閉會有警告
二、輸出
System.out.println("輸入的整數為:"+i);//不換行打印
System.out.println("輸入的浮點數為:"+d);
System.out.printf("輸入的字符串為:%s\n", sc);//按格式輸出
System.out.write(2222); //字節輸出,用着不方便所以不常用。