2017-07-25 169 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]; 
... 
}