【发布时间】:2023-03-19 13:21:01
【问题描述】:
private static int[] arr;
public static void inputarrays() {
Scanner scan = new Scanner(System.in);
System.out.println("length of an array");
int x = scan.nextInt();
arr = new int[x];
System.out.println("values of an array");
for(int n = 0; n < x; x++) {
arr[n] = scan.nextInt();
}
}
我查找了如何编写代码来获取用户输入并创建这样的数组: Java - Creating an array from user input
看起来我的代码与链接上的代码相同,这意味着它应该可以正常工作。但是,当我输入数组的值时,扫描仪永远不会关闭。我试过scan.close(),但没有用。另外,我不能对这个使用 try&exception。
【问题讨论】:
-
将 x++ 改为 n++
-
我改了,但扫描仪还是没有停止
-
在每个
scan.nextInt();之后添加这一行:scan.nextLine(); -
感谢您的编辑,下次我会按照格式进行
-
如果我在方法内部创建一个新数组而不是在数据字段中声明它,它仍然不起作用?