回 帖 发 新 帖 刷新版面

主题:帮帮看下这个程序

public class Primzahlen 
{
    
    public static void main(String[] args)
    {
        
        int A[]= new int[10000];
        int B[]=new int[1000];
        int m=0;
        for(int i=3;i<A.length;i+=2)
            A[i]=1;
        for(int i=3;i<B.length;i+=2)
            if(A[i]==1)
            {
                if(m++<B.length)
                B[0]=2;B[m]=i;
                 for(int j=i+i+i;j<A.length;j+=i+i)
                A[j]=0;
            }
        
        
        System.out.println("ersten 20 Prinmzahlen sind");
        for(m=0;m<20;m++)
            System.out.println(B[m]);
        
        
        System.out.println("ersten 50 Primzahlenzwillinge sind");
        for(int j=0;j<B.length;j++)
            if(B[j+1]-B[j]==2)
                if(m++<50)
                    System.out.println(B[j]+"und"+B[j+1]);
        
        
        System.out.println("die kleinste Primzahl ist");
        for(int n=0;n<B.length;n++)
            if(B[n+1]-B[n]>50)
                System.out.println(B[n]);
        
        
        
                    
        
   }
}    
这个是关于求质数的
总有下面这个错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1000
    at Primzahlen.main(Primzahlen.java:30)

回复列表 (共3个回复)

沙发

数组下标越界了
改成:
        for(int j=1;j<B.length;j++)
            if(B[j]-B[j-1]==2)

        for(int n=1;n<B.length;n++)
            if(B[n]-B[n-1]>50)
因为按照你原先的那样,B[i+1]在计算最后一个时,i+1==B.length

板凳

数组越界,比如:int A[]=new int[5];这样 数组A 可放 5 个整数,
是从A[0]-A[4],而A.length 等于 5 
所以在你的:
for(int j=0;j<B.length;j++)
            if(B[j+1]-B[j]==2)
中,最后一次循环是:
j=4,j+1 = 5
所以,你数组取到:B[j+1] = B[5]  而不是 B[4],这样就产生数组越界了


public class Primzahlen {
    public static void main(String[] args) {

        int A[] = new int[10000];
        int B[] = new int[1000];
        int m = 0;
        for (int i = 3; i < A.length; i += 2)
            A[i] = 1;
        for (int i = 3; i < B.length; i += 2)
            if (A[i] == 1) {
                if (m++ < B.length)
                    B[0] = 2;
                B[m] = i;
                for (int j = i + i + i; j < A.length; j += i + i)
                    A[j] = 0;
            }

        System.out.println("ersten 20 Prinmzahlen sind");
        for (m = 0; m < 20; m++)
            System.out.println(B[m]);

        System.out.println("ersten 50 Primzahlenzwillinge sind");
        
        /*for (int j = 0; j < B.length; j++){    
            if (B[j + 1] - B[j] == 2)
                if (m++ < 50)
                    System.out.println(B[j] + " und " + B[j + 1]);
        }*/
        for (int j = 1; j < B.length; j++){    
            if (B[j ] - B[j-1] == 2)
                if (m++ < 50)
                    System.out.println(B[j-1] + " und " + B[j]);
        }
            

        System.out.println("die kleinste Primzahl ist");
        
        /*for (int n = 0; n < B.length; n++){
            if (B[n + 1] - B[n] > 50)
                System.out.println(B[n]);
        }*/
        for (int n = 1; n < B.length; n++){
            if (B[n ] - B[n-1] > 50)
                System.out.println(B[n-1]);
        }    

    }
}

3 楼

非常谢谢两位

我来回复

您尚未登录,请登录后再回复。点此登录或注册