我这里有代码,可以通过一系列 for 循环尝试确定它是否是质数int
。vecArray
但是,当我运行程序时,我无法成功地将给定数组的每个素数找到/添加到提供的向量中。我错过了什么?
Vector<Integer> primeVec = new Vector<Integer>();
Vector<Integer> storage = new Vector<Integer>();
for ( int num : vecArray) {
//create array size of num
resultArray = new int[num];
for(int j = 1; j <= sqrt(num); j++) {
int result = num % j;
if (result == 0) {
storage.add(j);
}
}
//if resultArray has only two integers, then it must be prime
int size = storage.size();
if (size == 2) {
//add each
primeVec.add(num);
System.out.println("You added " + num + " to the Vector for primes!");
}
//print out all primes in vec
for (int e = 0; e < primeVec.size(); e++) {
System.out.println("The prime in element number " + e + " is: " + primeVec.get(e));
}
}
}
}