在Java中我相當新手,但我試圖運行這個簡單的代碼。有人能解釋我應該怎麼做才能使這個代碼有效嗎?簡單的Java代碼錯誤,線程「主」中的異常
public class BinaryGCD {
public static int gcd(int p, int q) {
if (q == 0) return p;
if (p == 0) return q;
// p and q even
if ((p & 1) == 0 && (q & 1) == 0) return gcd(p >> 1, q >> 1) << 1;
// p is even, q is odd
else if ((p & 1) == 0) return gcd(p >> 1, q);
// p is odd, q is even
else if ((q & 1) == 0) return gcd(p, q >> 1);
// p and q odd, p >= q
else if (p >= q) return gcd((p-q) >> 1, q);
// p and q odd, p < q
else return gcd(p, (q-p) >> 1);
}
public static void main(String[] args) {
int p = Integer.parseInt(args[0]);
int q = Integer.parseInt(args[1]);
System.out.println("gcd(" + p + ", " + q + ") = " + gcd(p, q));
}
}
在Eclipse中我得到以下錯誤:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at BinaryGCD.main(BinaryGCD.java:25)
重複的http://stackoverflow.com/questions/373328/invoking-java-main-method-with-parameters-from-eclipse http://stackoverflow.com/questions/7574543/how-to-pass- console-arguments-to-application-in-eclipse – Shashi