美团java后台第二道编程题目的输入
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[] strs = new String[N];
int i=0;
sc.nextLine();// 8 缺失的一行
for(i=0;i<N;i++){
strs[i] = sc.nextLine();
}
}
输入数据是下面这样的:
2
1,2
2,3,4
在线笔试时没有写第8行,导致下面for循环中的第一次sc.nextLine()读到空字符串""。
然后在后面使用Integer.parseInt(String arg0)时,总是出现 Format之类的错误。
sc.nextLine()的作用是 调到下一行,并返回跳过的数据 .
猜想第一个sc.nextInt()时,光标还是在第一行。
以后大家小心这个坑。
#美团#