public class StackOfIntegers { private int[] prime = new int[100]; private int divisor = 2; private int dividend = 0; private int total = 1;
public void setDividend(int dividend) { this.dividend = dividend; }
public static int isPrime(int i) {// 判断是否为素数 int k, j; k = 0; for (j = 2; j < i - 1; j++) { k = i % j; if (i > 3 && k == 0) break; } if (k != 0 || i == 2 || i == 3) { k = 1; } return (k); }
public int[] calculate() { int i = 0; while (total != dividend) { if (isPrime(divisor) == 0) { divisor++; continue; } if (dividend % divisor == 0) { total = total * divisor; prime[i] = divisor; i++; } else { divisor++; } } return prime; } } package stackOfInt;
import java.util.Scanner;
public class TestStack {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int integer, length; System.out.print("Please input a integer: "); Scanner input = new Scanner(System.in); integer = input.nextInt(); StackOfIntegers stack = new StackOfIntegers(); stack.setDividend(integer); int []prime = new int[100]; prime = stack.calculate(); length = prime.length; while(length > 0){ System.out.print(prime[length] + " "); length--; } } public static void compositor(int []prime){ int i, j, temp; for(i = 0; i < prime.length; i++){ for(j = 1; j < prime.length; j++){ if(prime[j] < prime[j-1]){ temp = prime[j]; prime[j] = prime[j-1]; prime[j-1] = temp; } } } }
} Please input a integer: 120 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100 at stackOfInt.StackOfIntegers.calculate(StackOfIntegers.java:36) at stackOfInt.TestStack.main(TestStack.java:19) 出现了数组越界问题?该怎么改啊。。。。