【发布时间】:2012-11-03 17:54:30
【问题描述】:
代码:
int size=0;
Scanner in = new Scanner(System.in);
System.out.println("Enter size of the Graph");
size = in.nextInt();
System.out.println(size);
for (int i = 1; i <= size; i++) {
Scanner in2 =new Scanner(System.in);
while(in2.hasNextInt()){
int num = in2.nextInt();
System.out.print("(" + i + "," + num + ")"+",");
System.out.println();
}
System.out.println("out of the while loop");
}
输入与输出:
Enter size of the Graph
4
4
2 3 4
(1,2),
(1,3),
(1,4),
2 5 6
(1,2),
(1,5),
(1,6),
如您所见,我的程序不存在 while 循环。它仍然打印 i=1 的值。 我做错了什么?
【问题讨论】:
-
然后你应该输入 EOF - 否则程序将不断等待下一个提要。你用的是windows还是linux?
-
Linux.我该怎么做?
-
提供任何字符输入。这会让你脱离循环。
-
试试 Control-D。我认为这会发送 EOF。
标签: java input while-loop java.util.scanner