java.lang.ArrayIndexOutOfBoundsException求救啊
packagestackOfInt;publicclassStackOfIntegers{privateint[]prime=newint[100];privateint...
package stackOfInt;
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)
出现了数组越界问题?该怎么改啊。。。。 展开
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)
出现了数组越界问题?该怎么改啊。。。。 展开
展开全部
int []prime = new int[100];
因为你定义的数组就是长度100的,你的参数又是system.in的方式从外部读入,那我如果故意输入100以上的数字,必定就抛出这个异常了。
可以这么改:
integer = input.nextInt();
之后,直接
int intNum = 0;
try{
intNum = Integer.parseInt(integer);
}catch(Exception e){
intNum = -1;
}
if(intNum == -1 || intNum > 100){
System.out.println("数值不合法");//至于多个提示信息你自己写
return;
}
因为你定义的数组就是长度100的,你的参数又是system.in的方式从外部读入,那我如果故意输入100以上的数字,必定就抛出这个异常了。
可以这么改:
integer = input.nextInt();
之后,直接
int intNum = 0;
try{
intNum = Integer.parseInt(integer);
}catch(Exception e){
intNum = -1;
}
if(intNum == -1 || intNum > 100){
System.out.println("数值不合法");//至于多个提示信息你自己写
return;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询