为什么我使用以下Java代码获得运行时错误?

时间:2016-11-04 09:58:38

标签: java debugging

我的代码有什么问题?我得到运行时错误。我是Java编程的新手,在编译以下代码时遇到了困难:

import java.util.Arrays;
import java.util.Scanner;

public class StoneDivision {

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

    long n = scan.nextLong();
    int m = scan.nextInt();
    long si[] = new long[m];

    for (int i = 0; i < m; i++) {
        si[i] = scan.nextLong();
    }

    Arrays.sort(si);

    if (calculateWin(n, m, si, si.length - 1, 1)) {
        System.out.println("First");
    } else {
        System.out.println("Second");
    }
}

static boolean calculateWin(long n, int m, long[] si, int siIndex, int count) {

    long siN = si[siIndex];

    if (n / siN == 0)
        return true;

    if (n == 1)

        if (n % siN == 0) {
            long nN = n / siN;
            count++;
        }

    return calculateWin(n, m, si, siIndex - 1, count);

}

}

我得到的错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at StoneDivision.calculateWin(StoneDivision.java:28)
    at StoneDivision.calculateWin(StoneDivision.java:40)
    at StoneDivision.calculateWin(StoneDivision.java:40)
    at StoneDivision.calculateWin(StoneDivision.java:40)
    at StoneDivision.main(StoneDivision.java:19)

这是ideone链接: http://ideone.com/g2C0ph

0 个答案:

没有答案