日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

[转载] Scanner和bufferreader读取控制台字符的区别

發布時間:2025/3/11 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载] Scanner和bufferreader读取控制台字符的区别 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考鏈接: Java中Scanner和BufferReader類之間的區別

從開始學習Java就用了scanner,因為比較簡單每當遇到空格鍵或者換行鍵則讀取下一個字符,一般用法?

? while(input.hasNextInt()){ int n = input.nextInt(); int t = input.nextInt(); int c = input.nextInt(); int[] a = new int[n]; for(int i = 0;i < n;i++){ a[i]=input.nextInt(); }?

?這樣就可以讀取若干行以空格鍵或者換行鍵輸入,但是今天做一個在線編程時發現bufferreader比scanner快,雖然比scanner占用內存多一些?

?bufferreader的輸入只能使用readline()的方式讀取一行文本,如果想將BufferedReader .readLine()的文本(默認是字符串類型)轉換成其他類型的話,需要調用相應的方法(比如說想換成int類型的話,調用Integer.parseInt(BufferedReader .readLine()方法去轉換格式))??

?

? BufferedReader br =?

? new?

? BufferedReader(

? new?

? InputStreamReader(System.in));

??

?

? ? ? ? ??

? String str;

??

?

? ? ? ? ??

? while

? ((str=br.readLine())!=

? null){

??

?

? ? ? ? ? ? ??

? String[] s = str.trim().split(

? " "

? );

??

?

? ? ? ? ? ? ??

? int?

? n = Integer.parseInt(s[

? 0

? ]);

??

?

? ? ? ? ? ? ??

? int?

? t = Integer.parseInt(s[

? 1

? ]);

??

?

? ? ? ? ? ? ??

? int?

? c = Integer.parseInt(s[

? 2

? ]);

??

?

? ? ? ? ? ? ??

? int

? [] value =?

? new?

? int

? [n];

??

?

? ? ? ? ? ? ??

? if

? ((str=br.readLine())!=

? null

? ){

??

?

? ? ? ? ? ? ? ? ??

? String[] s2 = str.trim().split(

? " "

? );

??

?

? ? ? ? ? ? ? ? ??

? for

? (

? int?

? i =

? 0

? ;i<n;i++){

??

?

? ? ? ? ? ? ? ? ? ? ??

? value[i] = Integer.parseInt(s2[i]);

??

?

? ? ? ? ? ? ? ? ??

? }

??

?

? ? ? ? ? ? ??

? }

??

?

? 2.trim()的用法

??

?

? trim是去掉字符串首尾的空格

??

?

? 3.parseInt()函數

??

?

? Integer.parseInt(String s)將string返回int數據

??

?

? Integer.parseInt(String s,int i)將i進制數據轉成10進制

??

?

轉載于:https://www.cnblogs.com/lxy1998/p/6775533.html

總結

以上是生活随笔為你收集整理的[转载] Scanner和bufferreader读取控制台字符的区别的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。