Java使用数组和分解打印不同的数字

时间:2015-06-29 04:41:00

标签: java arrays printing numbers distinct

如果有人可以查看我的代码,我们将不胜感激。

我几乎得到了我正在寻找的输出但是现在已经存在了,而且我很难将它分开看这么久。
我相信它可能与我的计数代码有关,数组[i]是否正确?

public static void main(String[] args) {
    //Variables
    int[] array=new int[10];

    //Object Scanner
    Scanner input = new Scanner(System.in);

    //Prompt user
    System.out.print("Enter ten numbers: ");

    for (int i=0; i<array.length; i++){
    array[i]=input.nextInt();
    }

    //Sort Array
    Arrays.sort(array);     

    //Count number of distincts
    int count =0 ;
    for(int i=0; i<array[i]; i++){

        count++;

    System.out.print("The number of distinct numbers is " + count);

    }

    //Display distinct numbers
    System.out.println(" ");
    System.out.print("The distinct numbers are ");
    for(int i=0; i<array[i]; i++){
        if(array[i] != -1){
            System.out.print(Arrays.toString(distinctNumbers(array)));
        }
    }
}

public static int[] distinctNumbers(int[] array){
     int[] distinctNumbers=new int[10];
        for(int i=0; i<array.length; i++){
            for(int j= i+1; j<array.length; j++){
            if(array[i] ==array[j]){
                    array[i]= -1;

            }
            }
        }
        return array;       
    }
}   

我目前得到的输出是:

Enter ten numbers: 1 2 3 4 3 2 1 2 3 4

The number of distinct numbers is 1 

The distinct numbers are [-1, 1, -1, -1, 2, -1, -1, 3, -1, 4]

期望的输出:

Enter ten numbers: 1 2 3 4 3 2 1 2 3 4

The number of distinct numbers is 4

The distinct numbers are [1, 2, 3, 4]

3 个答案:

答案 0 :(得分:0)

我看到错误的地方有两个地方:

System.out.print("The distinct numbers are ");
for(int i=0; i<array[i]; i++){
    if(array[i] != -1){      
    System.out.print(Arrays.toString(distinctNumbers(array)));
    }
}

在这里检查数组[i]并打印整个数组。我认为你应该检查array.length并按元素打印数组。我建议的代码是:

System.out.print("The distinct numbers are ");
array=distinctNumbers(array);         //operate on the array outside loop
for(int i=0; i<array.length; i++){           //iterate till array length
    if(array[i] != -1){      
    System.out.print(array[i]+" ");      //print element wise
    }
}

答案 1 :(得分:0)

   for(int i=0; i<array[i]; i++){

          count++;

     System.out.print("The number of distinct       numbers is " + count);

     }

我认为它错了..你需要工作到array.length ..然后找到不同的数字..那为什么计数返回1而不是4

然后在显示它们时使用条件如:if(array [i]!= -1)

答案 2 :(得分:0)

使用HashSet消除重复:

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

public class QuickTester {

    public static void main(String[] args) {

        int[] array = new int[10];
        Scanner input = new Scanner(System.in);

        System.out.print("Enter ten numbers: ");
        for (int i = 0; i < array.length; i++){
            array[i] = input.nextInt();
        }

        array = distinctNumbers(array);

        System.out.println("The number of distinct numbers is: " +
                array.length);

        System.out.print("The distinct numbers are: ");
        for(int number : array) {
            System.out.print(number + " ");
        }

        input.close();
    }

    public static int[] distinctNumbers(int[] array) {

        HashSet<Integer> distinctSet = new HashSet<>();

        for(int i = 0; i < array.length; i++){
            distinctSet.add(array[i]);
        }

        int[] distinctArr = new int[distinctSet.size()];
        int j = 0;
        for(Integer number : distinctSet) {
            distinctArr[j++] = number;
        }

        return distinctArr;
    }
}

<强>输入/输出:

Enter ten numbers: 1 2 3 4 3 2 1 2 3 4
The number of distinct numbers is: 4
The distinct numbers are: 1 2 3 4