mardi 20 juin 2017

My program should calculate prime numbers, but stops after the first number

So I wrote this little program that is supposed to check if a number is a prime number and if it's the case should add it to an arraylist. The problem is that it just adds the number 3 and then stops. Could somebody please explain me why it behaves like that?

import java.util.ArrayList;
public class main{
    public static void main(String args[]){
        ArrayList Primzahlen=new ArrayList();
        int current=1;
        boolean prim=true;
        for(int a=0;a<100;a++){
            for(int b=2;b<current;b++){
                if(current%b==0){
                    prim=false;
                }
                if(b==current-1){
                    if(prim==true){
                        Primzahlen.add(current);
                    }
                }
            }
            current++;
        }
        System.out.println(Primzahlen);
    }
}

Aucun commentaire:

Enregistrer un commentaire