例如:Jay Chung
用Scanner來輸入得到的結果就只有Jay;但如果使用BufferedReader,就可以完整取得字串。
使用 BufferedReader 需要注意的是意外處理(Exception Handling),得在main後方加上
throws IOException
package input; /*@author Jay */ import java.util.Scanner; import java.io.*; public class Input { public static void main(String[] args) throws IOException { int age = 0; String name; Scanner scanner = new Scanner(System.in); BufferedReader buf = new BufferedReader( new InputStreamReader(System.in) ); System.out.println("What's your name?\n>> "); name = buf.readLine(); System.out.println("How old are you?\n>> "); age = scanner.nextInt(); System.out.printf("Your name is %s. You are %d.", name, age).println(); } }
沒有留言:
張貼留言