-1

我正在尝试编写一个简单的程序,该程序给出数组大小为 40 的最大值、最小值、偶数个数、奇数个数和素数个数。

我写了以下代码:

package TESTING;
import java.util.*;

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

    int numbers[]= {1,5,-9,12,-3,89,18,23,4,-6,14,-5,87,17,47,-73,39,49,56,71,
                    3,0,-2,23,-8,45,63,71,5,29,-4,15,78,37,99,-66,44,94,65,73};  
    //Find minimum (lowest) value in array using loop  
    System.out.println("Minimum Value = " + getMinValue(numbers));  
    //Find maximum (largest) value in array using loop  
    System.out.println("Maximum Value = " + getMaxValue(numbers));  
    System.out.println("Even Numbers = " + even(numbers));
    System.out.println("Odd Numbers = " + odd(numbers));
    for(int number = 1; number<=numbers.length; number++){
    //print prime numbers only
    if(isPrime(numbers)){

       System.out.println("+ numbers");
    }
  }


//Find maximum (largest) value in array using loop  
public static int getMaxValue(int[] numbers){  
int maxValue = numbers[0];  
for(int i=1;i<numbers.length;i++){  
    if(numbers[i] > maxValue){  
        maxValue = numbers[i];  
    }  
}  
return maxValue;  
}  

 //Find minimum (lowest) value in array using loop  
 public static int getMinValue(int[] numbers){  
int minValue = numbers[0];  
for(int i=1;i<numbers.length;i++){  
    if(numbers[i] < minValue){  
        minValue = numbers[i];  
    }  
}  
return minValue;  
}  
//Find even numbers in array 
public static int even(int[] numbers)
{
int countLength = 0;

for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] % 2 == 0) {
        countLength++; 
    }
}

return countLength; 
}

public static int odd(int[] numbers)
{
int countOdd =0;
for (int i=0; i<numbers.length; i++) {
    if (numbers[i] %2 != 0){
        countOdd++;
    }
}
return countOdd;

}

public static boolean isPrime(int numbers[]){
     for(int i=2; i<numbers.length; i++){
   if(numbers.length%i == 0){
       return false; //number is divisible so its not prime
        }
     }
     return true; //number is prime now
 }    


}

没看懂问题出在哪里?它没有在输出中显示素数。我得到以下输出:

输出

Minimum Value = -73
Maximum Value = 99
Even Numbers = 14
Odd Numbers = 26
4

4 回答 4

0

这是 isPrime 的更好变体:

public static int[] getPrime(int[] someNumbers) {
  int[] temp = new int[someNumbers.length];
  int cnt = 0;
  for (int num : someNumbers) {
    int max = num / 2;
    for (int i = 2; i <= max; i++) {
      if (num % i == 0) {
        temp[cnt++] = num;
      }
    }
  }
  int[] result = new int[cnt];
  System.arrayCopy(temp, 0, result, 0, cnt);
  return result;
}

注意:要打印它,您必须遍历方法的结果并打印每个数字

public static void printArray(int[] anArray) {
  boolean firstRun = true;
  for (int num : anArray) {
    if (! firstRun) {
      System.out.print(", ");
    } else {
      firstRun = false;
    }     
    System.out.print("" + num);
  }
  System.out.println();
}
于 2013-02-22T07:53:36.593 回答
0

我认为这是问题所在。正确观察代码更改:-

for (int number = 0; number < numbers.length; number++) {
            // print prime numbers only
            if (isPrime(numbers[number])) {
                System.out.println("Prime= "+numbers[number]);
            }
        }

isPrime(number)应该使用而不是numbers[],然后你的isPrime()方法应该是

public static boolean isPrime(int number) {
        for (int i = 2; i < number; i++) {
            if (number % i == 0) {
                return false; // number is divisible so its not prime
            }
        }
        return true; // number is prime now
    }

进行这些代码更改。它会工作的。

于 2013-02-22T07:44:17.670 回答
0

您正在尝试打印素数..是吗?然后删除我在下面编辑的双引号。

试试这个:当你试图打印素数时,在 for 循环中。

if(isPrime(numbers[i])){

   System.out.println(numbers[i]);
}

代替

if(isPrime(numbers)){

   System.out.println("+ numbers");
}

并编辑你的 isPrime 函数。

public static boolean isPrime(int numbers){
    for(int i=2; i<numbers; i++){
        if(numbers%i == 0){
            return false; //number is divisible so its not prime
        }
    }
     return true; //number is prime now
}    
于 2013-02-22T07:44:36.643 回答
0

List您可以轻松地从数据中创建一个 javaArrays.asList()

对列表进行排序。您的第一个和最后一个元素现在是最小值和最大值。

使用CollectionUtils.countMatches()并编写一个 Predicate 来测试数字是否为 isPrime、isEven、isOdd...

祝你好运!

于 2013-02-22T07:45:36.420 回答