在数组中显示奇数值

时间:2015-10-08 18:38:06

标签: java arrays

我试图在数组中显示奇数,但每个数字只显示一次(即数字[3] = 3,3,1;只会显示3和1而不是3,3和1。)

这是我现在的代码,程序完全将创建一个用户输入的特定长度,然后计算数组中的最大最小值和奇数值。

import java.util.Scanner;

public class ArrayLab
{
static Scanner input = new Scanner (System.in);

public static void main(String[] args)
{
    System.out.println("Enter the number of numbers: ");
    final int NUMBER_OF_ELEMENTS = input.nextInt();

    double[] numbers = new double[NUMBER_OF_ELEMENTS];
    System.out.println("Enter the numbers: ");

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
    {
        numbers[i] = input.nextDouble();
    }
    input.close();

    double max = numbers[0];
    double min = numbers[0];

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
    {
        if (numbers[i] > max)
        {
            max = numbers[i];
        }
    }
    System.out.println("The max is: " + max);

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
    {
        if (numbers[i] < min)
        {
            min = numbers[i];
        }
    }
    System.out.println("The min is: " + min);

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
    {
        if (numbers[i] % 2 != 0)
        {
            System.out.println ("The odd numbers are: " + numbers[i]);
        }
    }

}

}

感谢您的帮助。

6 个答案:

答案 0 :(得分:3)

Set<Integer> set = new HashSet<Integer>();
 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
    {
     if (numbers[i] % 2 != 0)
        {
            set.add(numbers[i]);
        }
    }
System.out.println ("The odd numbers are: " +set);

答案 1 :(得分:3)

使用Java8可以做得更简单:

double[] d = Arrays.toStream(numbers).filter(d -> (d % 2) == 1).distinct().toArray();

for(double tmp : d)
    System.out.println(tmp);

System.out.println("min: " + Arrays.toStream(numbers).min((a , b) -> new Double(a).compareTo(b)));

System.out.println("max: " + Arrays.toStream(numbers).max((a , b) -> (new Double(a).compareTo(b))));

对于您的解决方案:您永远不会消除重复数字,因此重复数据会保留在数组中,直到您打印所有奇数和最大数字。

这种消除可以通过以下几种方式完成:

  • 使用上述Java8
  • 将所有值添加到Set,因为这些值不允许重复值
  • 以自己的方式消除它们(我不会为此提供任何代码,因为为此设计有效的解决方案相当复杂)

答案 2 :(得分:0)

根据您的需要更新解决方案。请使用更好的编码标准。请注意条件检查!oddNumbers.contains(numbers [i])不是很必要,因为HashSet从不接受任何重复值。

import java.util.HashSet;
import java.util.Scanner;

public class ArrayLab {
static Scanner input = new Scanner(System.in);

public static void main(String[] args) {
    System.out.println("Enter the number of numbers: ");
    final int NUMBER_OF_ELEMENTS = input.nextInt();

    double[] numbers = new double[NUMBER_OF_ELEMENTS];
    System.out.println("Enter the numbers: ");

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
        numbers[i] = input.nextDouble();
    }
    input.close();
    HashSet<Double> oddNumbers = new HashSet<Double>(NUMBER_OF_ELEMENTS);

    double max = numbers[0];
    double min = numbers[0];

    for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
        if (numbers[i] > max) {
            max = numbers[i];
        }

        if (numbers[i] < min) {
            min = numbers[i];
        }

        if (numbers[i] % 2 != 0 && !oddNumbers.contains(numbers[i])) {
            oddNumbers.add(numbers[i]);
        }
    }
    System.out.println("The max is: " + max);
    System.out.println("The min is: " + min);
    System.out.println("The odd numbers are: " + oddNumbers);
}

}

答案 3 :(得分:0)

对您的方法更有意义的解决方案如下:

int[] tempArray; //temporary array to store values from your original "array"
int count=0;
for(int i=0; i<numbers.length; i++) {
    if(numbers[i]%2 != 0) {
        count++;
    }
}
tempArray = new int[count]; //initializing array of size equals to number of odd digits in your array
int j = 0;
for(int i=0; i<numbers.length; i++) {
    boolean check = true;
    for(int k=0; k<j; k++) {
        if(tempArray[k] == numbers[i]) {
            check = false; //this will prevent duplication of odd numbers
        }
    }
    if(numbers[i]%2 != 0 && check) {
        tempArray[j]=numbers[i];
        j++;
    }
}
//Now print the tempArray which contains all the odd numbers without repetition

答案 4 :(得分:0)

有些人提到了套装,但也有不同的方式。只需对数组进行排序,然后扫描它,检查最后一个打印的每个数字。即,

int lastPrinted = 0;

// Sort the array
Arrays.sort(numbers);

System.out.print("The odd numbers are: ");

// Scan through the array
for (int i = 0; i < NUMBER_OF_ELEMENTS; i++)
{
    // if it's odd and doesn't match the last one...
    if (numbers[i] % 2 != 0 && numbers[i] != lastPrinted)
    {
        // ...print it and update lastPrinted
        System.out.print( "" + numbers[i] );
        lastPrinted = numbers[i];
    }
}
System.out.println("");

作为旁注,你真的不需要扫描数组两次以找到你的最大值和最小值,你可以一次性完成。

答案 5 :(得分:0)

我认为你可以使用内置的hashmap类及其方法来实现任务,而不会在很大程度上影响算法的复杂性。

import java.util.HashMap;

public class Hashing {

public static void main(String[] args) {
    //declare a new hasmap
    HashMap<Integer, Integer> map = new HashMap<>();
    //consider Arr as your Array
    int Arr[] = {3,3,1,4,5,5,7,8};
    //traverse through the array
    for(int i=0;i<Arr.length;i++){
        //check if the required condition is true
        if(Arr[i]%2==1){
            /*now we insert the elements in the map but before
              that we have to make sure that we don't insert duplicate values*/
            if(!map.containsKey(Arr[i])){// this would not affect the complexity of Algorithm since we are using hashMap
                map.put(Arr[i], Arr[i]);//We are storing the Element as KEY and as VALUE in the map
            }
        }
    }
    //now We can get these element back from map by using the following statement
    Integer[] newArray = map.values().toArray(new Integer[0]);
    //All the required elements are now present in newArray
    for(int ele:newArray){
        System.out.println(ele);
    }
}

}