2017-07-25 172 views
1

所以这段代码取n的值并返回一个除数列表以及除数的总数。如果我删除了Scanner声明并将其赋值给int n并只给出一个int值,则代码将完美运行。为什么扫描仪功能无法正常工作?

然而,因为它是,它返回:

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"); 


} 
} 
+0

观察你的病情的同时,'X <= N'。记住数组索引从0开始不是1. –

+2

你声明你的int []'arr'长度为'n'。但是,您不会给'n'一个值,所以默认情况下它是0.因此,'arr'的长度为0,所以您无法得到ArrayIndexOutOfBounds异常。 – geokavel

+1

假设您没有为'static int n'分配一个值,因此如果默认情况下为0,那么'arr'的大小。还有一个局部变量'int n',而不是'static' –

回答

2

arr大小声明时n仍持有任何值(前大小被输入)。这样做:

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 
      //... 
     } 
    } 
} 

你需要你输入n来命名的arr大小,否则大小设置为0,造成ArrayIndexOutOfBoundsException

这条线:

arr[q] = p; 

是实际造成的错误。 arr[q]无法保留一个值,因为arr[q]。阵列没有大小,所以它不能容纳任何成员。

+0

哦...是的!我错过了那部分,你明白了,这是一个加号 –

+0

有道理。出于某种原因,我在考虑给n值以某种方式追溯调整数组的长度。轻松修复。 – user8360486

+0

@ user8360486是的。没问题 – CodingNinja

0

while(x <= n) 

while(x < n) 

<不仅仅意味着较为宽松的更改而病情,让你从1开始,而不是让出界

编辑:

另外,作为@CodingNinja说,你必须改变,并定义一个int值的数组的大小,默认为0:

public static void main(String[] args){ 
    static int n; 
    Scanner scan = new Scanner(System.in); 
    int n = scan.nextInt(); 


    static int [] arr = new int[n]; 

    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"); 


    } 
+0

仍然不起作用... – CodingNinja

+0

@CodingNinja是的......我怎么能错过它?非常感谢 –

+0

有你去,+1为你 – CodingNinja

1
static int n; 
... 
static int [] arr = new int[n]; 

你不给n的值,所以它默认为0。因此,你初始化arr为长度为0的数组这就是为什么你Exception in thread "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]; 
... 
}