牛客網的ACM模式需要自己寫輸入輸出,在這里簡單記錄一下:
基本答題框架:
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
// ...
sc.close();
}
}
常見的輸入形式:
next()、nextLine()、nextInt()、nextFloat()、nextDouble();
對于數字型別的輸入比較好懂,有坑的是 next() 和 nextLine();
Q:
next()和nextLine()有什么區別?A:
next()會自動消掉有效字符前的空格,只回傳輸入的字符,不能得到帶空格的字串;next()在輸入有效字符之后,將其后輸入的空格鍵、Tab鍵或Enter鍵等視為分隔符或結束符;nextLine()方法回傳的是 Enter 鍵之前的所有字符,它是可以得到帶空格的字串的;nextLine()的結束符只有 Enter 鍵;
注意點:
在每一個 next()、nextDouble() 、nextFloat()、nextInt() 等陳述句之后加一個 nextLine() 陳述句,將被 next() 去掉的Enter結束符過濾掉,
舉個例子??:
import java.util.*;
/*
用例輸入:
1
2
a b c
*/
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int num1 = sc.nextInt();
int nums2 = sc.nextInt();
sc.nextLine(); // 容易漏掉,不加這一句,輸入完2,程式直接往下走了,下一句字串跳過了
String a_b_c = sc.nextLine();
// 這樣才能讀取到正確的內容,
// 不加sc.nextLine()的話,a_b_c是一個Enter字符,
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/502229.html
標籤:其他
上一篇:XXE漏洞學習
下一篇:leetcode 304. Range Sum Query 2D - Immutable 二維區域和檢索 - 矩陣不可變(中等)
