我正在尝试编写一个简单的程序,它给出数组大小为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
答案 0 :(得分: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
}
更改这些代码。它会工作。
答案 1 :(得分: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
}
答案 2 :(得分:0)
您可以使用List
Arrays.asList()
对列表进行排序。您的第一个和最后一个元素现在是最小值和最大值
使用CollectionUtils.countMatches()并编写一个谓词来测试数字是否为Preme,isEven,isOdd ......
祝你好运!答案 3 :(得分: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();
}