打印不同数字java

时间:2015-02-12 03:54:24

标签: java arrays

我在使用此代码时遇到了困难。代码完成但输出错误。 我的代码打印输入十个数字:1 2 3 5 6 6 8 7 4 1 它应该打印 不同的数字是: 1 2 3 5 6 8 7 4 但它并没有。它打印: 10 10 10 10 10 7 我该如何解决?



Here is my code:

    import java.util.*;
    
    public class homework1 {
            public static void main(String[] args){
                    // input from user
                    Scanner input = new Scanner(System.in);
                    int [] numbers = new int[10];
                    boolean[] distinct = new boolean[10];
                    System.out.println("Enter ten numbers");
                    for (int i=0; i<numbers.length; i++){
                            System.out.println("Number " + (i + 1) +":  ");
                            numbers [i] = input.nextInt();
                            distinct[i] = true;
                            for(int j = 0;j<10; j++){
                                    if(numbers[i] == numbers[j] && i != j) {
                                            distinct[i] = false;
                                            }
                                    }
                            }
    
                    int count=0;
                    for(int j = 0;j<10; j++){
                         if (distinct[j]){
                              numbers[count]=distinct.length;
                              count++;
                         }
                    }
                    System.out.println("The number of distinct number is: "+numbers[count]);
                    System.out.println("The distinct numbers are: ");
                    for(int i= 0; i < 10; i++) {
                            if(distinct[i]) {
                                    System.out.print(numbers[i] + " ");
                                    }
                            }
                            System.out.println();
            }
    }
&#13;
&#13;
&#13;

5 个答案:

答案 0 :(得分:1)

这一行:

numbers[count]=distinct.length;

将输出设置为数组的长度(在本例中硬编码为10.尝试:

int count=0;
for(int j = 0;j<10; j++){
    if (distinct[j]){
        numbers[count]=numbers[j];
        count++;
    }
}

将输出设置为不同的数字。

插头:查看Code Review

答案 1 :(得分:0)

这将解决您的问题。 如果使用HashMap,则代码较少。

import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class Test {

    public static void main(String[] args) {

        HashMap<Integer, String> distinctNumbers = new HashMap<Integer, String>();
        // input from user
        Scanner input = new Scanner(System.in);
        System.out.println("Enter ten numbers");
        for (int i = 0; i < 10; i++) {
            System.out.println("Number " + (i + 1) + ":  ");
            Integer numberEntered = input.nextInt();
            distinctNumbers.put(numberEntered, null);
        }

        System.out.println("The number of distinct number is: " + distinctNumbers.size());
        System.out.println("The distinct numbers are: ");
        Set<Integer> keys = distinctNumbers.keySet();
        for (Iterator<Integer> iterator = keys.iterator(); iterator.hasNext();) {
            Integer integer = (Integer) iterator.next();
            System.out.print(" "+ integer);

        } 
    }

}

答案 2 :(得分:0)

删除行numbers[count] = distinct.length;这使您的输出全部为10

然后,我稍微改变你的代码以使其正常运行:

...
int count = 0;
    for (int j = 0; j < 10; j++) {
        if (distinct[j]) {
            //numbers[count] = distinct.length; //remove it, it nonsense 
            count++;
        }
    }
// distinct number should be count, not number[count]
System.out.println("The number of distinct number is: " + count);
    ....

答案 3 :(得分:0)

public static void main(String[] args) {
        int arr[] = {1,3,5,4,7,3,4,7};
        Map<Integer, Integer> frequency = new HashMap<Integer, Integer>();
        for(int i = 0; i < arr.length; i++) {
            if(frequency.containsKey(arr[i])){
                int value = frequency.get(arr[i]);
                frequency.put(arr[i], value + 1);
            }
            else {
                frequency.put(arr[i], 1);
            }
        }
        for(Map.Entry<Integer, Integer> entry : frequency.entrySet()) {
            System.out.println(entry.getKey());
        }
    }

答案 4 :(得分:0)

package Chapter7;

import java.util.Scanner;

public class Exercise7_5 {

    public static void main(String[] args) {
        // Print distinct numbers

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

        System.out.println("Enter 10 numbers:  ");
        for (int i = 0, j = 0; i < 10; i++) {
            if (storeDistinctNumbers(j,input.nextInt(), numbers))
                j++;
        }

        for (int i = 0; numbers[i] != 0 ; i++)
            System.out.print(numbers[i] + " ");
    }

    public static boolean storeDistinctNumbers(int j, int num, int[] numbers) {
        for (int e: numbers) {
            if (e == num)
                return false;
        }
        numbers[j] = num;
        return true;    
    }
}