#How many Primenumber are there in one Arry

1 messages · Page 1 of 1 (latest)

trail flame
#

I need help i am new into programming and i want to programm smth that counts how many primenumber there are in one array. But the Primenumbercounter counts it wrong and i know why but not how too change it:
public static int numOfPrime (int[] arr){
int primeCount = 0;
boolean isPrime = true;
for(int i = 0; i < arr.length; i++){
for(int d = 2; d <= arr[i]/2; d++){
if(arr[i] % d == 0){
isPrime = false;
if(arr[i] == 2){
isPrime = true;
}
}else{
isPrime = true;
}
if (isPrime == true){
primeCount++;
}
}
}
return primeCount;
}

woven oreBOT
#

<@&987246399047479336> please have a look, thanks.

thorn cairn
#
   public static int numOfPrime (int[] arr){
      int primeCount = 0;
      boolean isPrime = true;
      for(int i = 0; i < arr.length; i++){
         for(int d = 2; d <= arr[i]/2; d++){
            if(arr[i] % d == 0){
               isPrime = false;
               if(arr[i] == 2){
                  isPrime = true;
               }
            }else{
               isPrime = true;
            }
            if (isPrime == true){
               primeCount++;
            }
         }
      }
      return primeCount;
   }```
#

your last check needs to be outside the inner for

#

But I suggest making a separate isPrime function that takes a number

trail flame
#

oh my good ty i have got so many other weird ansers who just changed my whole code and i undersatnd this answer more because i just made a simple mistake