dimanche 21 février 2016

Stop loop once if value is met one time

I have a for loop checking the result of value % i. Inside, there is an if statement checking if value % i = 0. If it does equal zero, it sets boolean isDivisible to true. If it returns a nonzero it sets isDivisible to false. The issue is that, for example, if the value is 12, some attempts will return 0 while some will be nonzero, so isDivisible is constantly changing inaccurately. I want the loop to end once value % i = 0. Is such a thing possible?

for (int i=2; i < value; i++){
            if (value % i == 0){
                isDivisible = true;
            }
            else{
                isDivisible = false;
            }
        }
        if (isDivisible = true){
                System.out.println(value + " is prime");
            }

Aucun commentaire:

Enregistrer un commentaire