1

我在这里不知所措。

我有这个家庭作业,我必须让用户输入 10 个数字,将它们放在一个数组中,并找出哪些输入的数字是唯一的。

这是我现在的工作流程:输入数字>如果之前没有输入数字,则存储在数组中;如果之前输入过数字,忽略>显示输入的数字>显示唯一的数字

例如:输入 1 2 3 5 1 2 4 6 将找到唯一数字并显示“1 2 3 4 5 6”

到目前为止,我的代码如下所示:

public class HwChapter6 {
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);

        int[] count = new int[10];
        int number = 0;
        int x = 0;
        boolean unique = false;
        int length = count.length;
        System.out.println("Insert 10 single digit numbers in any order your heart desires:");
        for (int i = 0; i < count.length; i++) {
            count[i] = input.nextInt();
            number = count[i];
            for (int j = 0; j < count.length; j++) {

谢谢你们的帮助。

4

4 回答 4

2

而不是输入值的数组,将它们放在 a Setof 中Integers。根据定义,集合仅存储唯一值。如果添加 3 个 'foo',则集合中将只有一个 'foo'。

// Add this to your top-level loop
Set<Integer> uniqueValues = new TreeSet<Integer>;
uniqueValues.add(number);

// Add this after the loop to write all unique values on one line
for (Integer value : uniqueValues) {
  System.out.print(value.toString() + " ");
}

// Now end the line.
System.out.println();
于 2013-09-19T02:09:40.133 回答
1

将所有数字存储在一个数组中。对于每个存储的数字:检查之前是否插入了数字并将其保存在布尔数组中。打印布尔数组中未标记的所有数字。

java.util.Scanner input = new java.util.Scanner(System.in);

int[] numbers = new int[10];
boolean[] usedBefore = new boolean[10];

// Insert all numbers
for (int i = 0; i < numbers.length; i++) {
    // Read number from console
    numbers[i] = input.nextInt();

    // Check if number was inserted before
    usedBefore[i] = false;
    for(int k = 0; k < i; k++) {
        if(numbers[k] == numbers[i]) {
            usedBefore[i] = true;
            break;
        }
    }
}

// Print all numbers that were not inserted before
for(int j = 0; j < numbers.length; j++) {
    if(!usedBefore[i]) {
        System.out.print(String.valueOf(numbers[j])+" ");
    }
}
于 2013-09-19T02:29:30.417 回答
1

检查输入的数字,然后通过标记第二个(布尔)数组中的相同位置来跟踪哪些是唯一的,true如果它们是唯一的,false则不然。

然后,当您打印出唯一值时,仅numbers[]uniques[]包含的每个位置打印该值true

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

for(int i = 0; i < 10; i++) {
    System.out.println("Please enter a value: \n" + "[" + (i + 1) + "]: ");
    numbers[i] = input.nextInt();
    uniques[i] = true;
    for(int j = 0; j < 10; j++) {
        if(numbers[i] == numbers[j] && i != j) {
            uniques[i] = false;
        }
    }
}

System.out.println("\nThe numbers you entered were: \n");
for(int i = 0; i < 10; i++) {
    System.out.println(numbers[i] + ", ");
}
System.out.println("done.\n\n");

System.out.println("\nThe uniqe numbers you entered were: \n");
for(int i = 0; i < 10; i++) {
    if(uniques[i]) {
        System.out.println(numbers[i] + ", ");
    }
}
System.out.println("done.\n\n");
于 2013-09-19T05:36:53.077 回答
0

最快、最简洁、最有效的方法是在完成所有其他操作之后,使用数组的第一个数字作为魔术值来破坏性地解析数组的唯一性:

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

for(int i = 0; i < 10; i++) {
    System.out.println("Please enter a value: \n" + "[" + (i + 1) + "]: ");
    numbers[i] = input.nextInt();
}

System.out.println("\nThe numbers you entered were: \n");
for(int i = 0; i < 10; i++) {
    System.out.println(numbers[i] + ", ");
}
System.out.println("done.\n\n");

System.out.println("The unique numbers are: ");
magic = numbers[0];
System.out.println(magic + ", ");
for(int i = 0; i < 10; i++) {
    for(int j = 0; j < 10; j++) {
        if(numbers[i] == numbers[j] && j != i) {
            numbers[j] = magic;
        }
    }
    if(numbers[i] != magic) {
        System.out.println(numbers[i] + ", ");
    }
}
System.out.println("done.\n\n");

Yes, I have two answers - this one is significantly different from the other one, and is better, though it is much more difficult for beginners to understand. Both solutions are valid, however.

于 2013-09-19T05:53:12.693 回答