我无法进入第二个循环,请告诉我如何解决这个问题?我又添加了 1 台扫描仪,但有更好的解决方案吗?
public class Include {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Integer> input1 = new ArrayList<>();
List<Integer> input2 = new ArrayList<>();
System.out.println("Введите последоватtльность чисел ");
while (sc.hasNextInt()) {
input1.add(sc.nextInt());
}
System.out.println(input1);
System.out.println(" введите вторую последовательность");
while (sc.hasNextInt()){
input2.add(sc.nextInt());
}
}
}
在这段代码中,每个序列的输入被限制为任何“不喜欢”整数的标记/元素——然后循环
while (sc.hasNextInt())终止,因为它们找不到最接近的整数。因此,第一个循环,偶然发现了一个“非数字”,完成了,但是这个分隔符在输入流中仍然是未读的,因此第二个循环也检测到它并退出。因此,为了“进入”第二个循环,你只需要读取这个分隔符,例如,加上
sc.next();--,任何非空白字符的序列都会被读取。结论: