我正在尝试使用for循环获取集合中的输入,但不知何故输出显示第0个位置为空
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
ArrayList<String> b = new ArrayList<String>();
String abc;
for (int i = a; i > 0; i--) {
abc = sc.nextLine();
b.add(abc);
}
我预计输出结果为:
[a, d, b, c, e]
但实际输出是:
[, a, d, b, c]
发布于 2019-05-17 02:44:13
添加sc.nextLine()
以跳过enter键:
Scanner sc = new Scanner(System.in);
int a;
String abc;
a = sc.nextInt();
sc.nextLine();
ArrayList<String> b = new ArrayList<String>();
for (int i = a; i > 0; i--) {
abc = sc.nextLine();
b.add(abc);
}
https://stackoverflow.com/questions/56174674
复制相似问题