将用户输入填充但不允许重复元素的数组

时间:2015-11-18 14:24:53

标签: java

我正在尝试编写一份我为作业编写的程序的一部分。程序的这一部分应该占用8个用户输入的整数,但是如果用户输入已经输入的数字,则显示错误并要求用户输入不同的数字。到目前为止,我还没有编写错误代码和第二次尝试代码的一部分,因为我遇到了检查重复部分的问题。目前,无论我投入什么,输出都是真的。

int maxNum = 8;
int[] arrayIn = new int[maxNum];
boolean duplicates = false;

// Run while not true
while (!duplicates) {
    // For loop for input into array
    for (int j = 0; j < maxNum; j++) {
        System.out.println("Enter digit " + digCounter + ":");
        arrayIn[j] = in1.nextInt();
        digCounter++;
        // Check for duplicates
        for (int a = 0; a < maxNum; a++) {
            for (int k = a + 1; k < maxNum; k++) {
                if (k != a && arrayIn[k] == arrayIn[a]) {
                    // Quit loop if duplicate found
                    duplicates = true;
                }
            }
        }

    }
    System.out.println(duplicates);
}

2 个答案:

答案 0 :(得分:2)

您应该查看名为Set的集合。有一个非常方便的函数Set.contains将返回truefalse,具体取决于Set中是否已有数字。像这样的东西可以解决这个问题:

Scanner scanner = new Scanner(System.in);
int maxNum = 8;
Set<Integer> numbers = new HashSet<>();
int digCounter = 1;

//Run while we need numbers
while (digCounter <= maxNum) {
    System.out.println("Enter digit " + digCounter + ":");
    int tempNumber = scanner.nextInt();
    if (numbers.contains(tempNumber)) { //If number is already chosen
        System.out.println("Sorry that number has already been added");
    } else { //If new number
        digCounter++;
        numbers.add(tempNumber);
    }
}
System.out.println(numbers);

或者,如果您只使用数组,则可以执行以下操作:

Scanner scanner = new Scanner(System.in);
int maxNum = 8;
int[] arrayIn = new int[maxNum];
boolean duplicate;
int digCounter = 1;

// Run while we need numbers
while (digCounter <= maxNum) {
    //reset duplicate
    duplicate = false;
    //Get user input
    System.out.println("Enter digit " + digCounter + ":");
    int temp = scanner.nextInt();
    for (int i = 0; i <= digCounter - 2; i++) { //Loop through accepted numbers
        if (temp == arrayIn[i]) { //We have found a match
            duplicate = true;
            break;
        }
    }
    //Check if duplicate
    if (duplicate) {
        System.out.println("Sorry that number has already been added");
    } else {
        arrayIn[digCounter - 1] = temp;
        digCounter++;
    }
}
System.out.println(Arrays.toString(arrayIn));

希望这有帮助!

答案 1 :(得分:0)

如果您不想使用set,此代码也可以使用。

    // creating the array
    int[] array = new int[10];

    // Taking the first number
    System.out.println("Please enter your numbers: ");
    array[0] = input.nextInt();

    // taking other inputs
    int count;
    for (count = 1; count < array.length; count++) {
        int num = input.nextInt();

        // Scanning the array for the number
        int n;
        for (n = 0; n < count; n++) {
            while (array[n] == num) { // This will also work if the user
                                        // wants to give duplicate after
                                        // duplicate
                System.out.println("You have entered this number before! Add another..");
                num = input.nextInt();
                n = 0;
            }

        }
        array[count] = num;

    }