Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么扫描仪功能不工作?_Java_Arrays_Java.util.scanner - Fatal编程技术网

Java 为什么扫描仪功能不工作?

Java 为什么扫描仪功能不工作?,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,这段代码取n的值,返回除数列表和除数总数。如果我删除Scanner声明及其对int n的赋值,并简单地给int n一个值,那么代码运行得很好 但实际上,它返回以下内容: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Program.main(Program.java:25) 我不知道是什么问题 import java.util.Scanner; public class Program

这段代码取n的值,返回除数列表和除数总数。如果我删除
Scanner
声明及其对int n的赋值,并简单地给int n一个值,那么代码运行得很好

但实际上,它返回以下内容:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at Program.main(Program.java:25)
我不知道是什么问题

import java.util.Scanner;
public class Program{
    static int n;
    static int x = 1;
    static int [] arr = new int[n];
    static int q = 0;
    static int g = 0;
    static int p = 1;
    static int count;


    public static void main(String[] args){

         Scanner scan = new Scanner(System.in);
         int n = scan.nextInt();

         while(x <= n){

            arr [q] = p; //assigns value to each array index
            g = n%arr[q]; // stores value of remainder
            q++; 
            p++;
            x++;
            if (g == 0){ //counts and displays each time remainder  = 0
                count++;
                System.out.println(q);
            }

        }

        System.out.println(count + " Divisors");


}
}
import java.util.Scanner;
公共课程{
静态int n;
静态int x=1;
静态int[]arr=新int[n];
静态int q=0;
静态int g=0;
静态int p=1;
静态整数计数;
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
int n=scan.nextInt();

while(x从

while(x <= n)

while(x当
n
仍然没有值时(在输入大小之前),
arr
的大小被声明。执行以下操作:

import java.util.Scanner;

public class Program {
    static int n;
    static int x = 1;
    static int [] arr; //no size set
    //...
    //Other variables
    //...

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        arr = new int[n]; //Now the size is set to the inputted number.
        while(x <= n) {
            //...
            //Other code to find divisors
            //...
        }
    }
}
是实际导致错误的原因。
arr[q]
无法保存值,因为没有
arr[q]
。数组没有大小,因此无法保存任何成员

static int n;
...
static int [] arr = new int[n];
您没有给
n
一个值,因此它默认为0。因此,您将
arr
初始化为长度为0的数组。这就是为什么在线程“main”java.lang.ArrayIndexOutOfBoundsException中出现
异常:0
。这是因为数组的大小为0,所以即使索引0也超出了数组的范围

如果您在从扫描仪读取之前不知道
n
,则应将代码更改为:

public static void main(String[] args){

     Scanner scan = new Scanner(System.in);
     int n = scan.nextInt();
     int [] arr = new int[n];
 ...
 }

观察while条件,
x声明int[]
arr
的长度为
n
。但是,您没有给
n
一个值,因此默认值为0。因此,
arr
的长度为0,难怪您会得到ArrayIndexOutOfBounds异常。假设您没有为
静态int n
赋值,因此默认情况下,
arr
的大小为0。Al所以有一个局部变量
int n
而不是
static
哦…是的!我错过了那部分,你知道了,它是一个加号是有意义的。出于某种原因,我认为给n一个值可以追溯调整数组的长度。很容易修复。@user8360486是的。不problem@CodingNinja是的…我怎么会错过呢?非常感谢大部分时间,给你+1
arr[q] = p;
static int n;
...
static int [] arr = new int[n];
public static void main(String[] args){

     Scanner scan = new Scanner(System.in);
     int n = scan.nextInt();
     int [] arr = new int[n];
 ...
 }