-
String類型讀取
import java.util.Scanner; // 注意類名必須爲 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的區別 while (in.hasNext()) { // 注意 while 處理多個 case String[] arr=in.nextLine().split(" "); //讀取輸入的一行數據並按空格 System.out.println(arr[0]); } } }
-
int類型讀取
import java.util.Scanner; // 注意類名必須爲 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的區別 while (in.hasNextInt()) { // 注意 while 處理多個 case int a = in.nextInt(); int b = in.nextInt(); System.out.println(a + b); } } }
-
指定輸入個數
import java.util.*; // 注意類名必須爲 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的區別 int N = in.nextInt(); //要求輸入個數爲N TreeSet set=new TreeSet();//set集合去重排序 for(int i=0;i<N;i++){ set.add(in.nextInt()); } Iterator it=set.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
-
next() 與 nextLine() 區別
next():1、一定要讀取到有效字符後纔可以結束輸入。 2、對輸入有效字符之前遇到的空白,next() 方法會自動將其去掉。 3、只有輸入有效字符後纔將其後面輸入的空白作爲分隔符或者結束符。
next() 不能得到帶有空格的字符串。
nextLine():1、以Enter爲結束符,也就是說 nextLine()方法返回的是輸入回車之前的所有字符。 2、可以獲得空白。
牛客網筆試的時候輸入輸出
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.