java以前的筆記(三)
java的輸入大家知道java中的輸出語句是System.out.println(”寫上要輸出的語句“);
可有沒有知道java中的輸入語句又是怎樣的呢?
找了很多的資料終于被我找到了點頭緒了··
哈哈 ··好高興···
看看這個的輸入 ·
public static void main(String[] args) throws IOException{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("Enter the number: ");
String text = input.readLine();
}
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("Enter the number: ");
String text = input.readLine();
}
這段代碼實在太長了·
我想簡化點·
能不能不要那個throws IOException 把那個去掉··結果是程序無法編譯的結果。
后來又想··在網上尋找別的更簡便的輸入方法··
終于看到了
Java5.0以后可以使用java.util.Scanner類:
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
long l = sc.nextLong();
float f = sc.nextFloat();
double d = sc.nextDouble();
String s = sc.nextLine();
int i = sc.nextInt();
long l = sc.nextLong();
float f = sc.nextFloat();
double d = sc.nextDouble();
String s = sc.nextLine();
這個多簡單啊··
來得實在··
這樣就簡單多了··
posted on 2009-07-16 09:14 duduli 閱讀(927) 評論(0) 編輯 收藏 所屬分類: java