将数据从键盘存储到数组而忽略重复

时间:2017-12-06 02:51:57

标签: java arrays java.util.scanner

所以我有一个我作为作业工作的程序,部分要求是要求我调用一个方法从用户检索数字,并将它们存储到array的大小20。我不得不在数组中包含任何重复值时执行此操作,并且我将继续存储用户数字,直到数组已满,或者用户输入了负值。我遇到了几个问题。

  1. 用户必须能够通过多行文本输入数据(使用'输入'键)而不会中断,例如:

    Enter the integers for the array: 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 18 18 18 18 19 20

    这应该将1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20存储到数组中而不会出现重复值。该程序不接受任何其他值,因为已输入20个唯一数字,这些数字填充了数组。 我尝试使用不同的选项,例如nextInt()nextLine(),甚至将数字存储为列表,然后进行转换,但我遇到了很多问题。

  2. 所以我的问题是,如何允许用户在多行输入中输入值,以及检查键盘缓冲区中的当前值是否为重复/负数,因此我可以知道在缓冲区中跳过该值或结束该计划?

    我会包含我的代码,但说实话,我已经尝试过多次重写,这绝对是一团糟。任何帮助都将受到欢迎。

        public static int getData(int [] set){
        Scanner keyboard = new Scanner(System.in);
    
        System.out.println("Enter the integers for the array.");
        System.out.println("The program will stop accepting values when you enter a negative number or reach the 20 unique limit.");
    
        int usrInput=keyboard.nextInt(), arraySizeUsed = 0;
    
        for(int position = 0; position < 20; ) { 
    
            if(!Arrays.asList(set).contains(usrInput)) {
                if(usrInput > -1) {
    
                    set[position] = usrInput;
                    position++;
                    arraySizeUsed++;
                }
    
                else {
                    return arraySizeUsed;
                }
            }
            else {
                continue;
            }
        }
    
        return arraySizeUsed;
    }
    

    我不认为我应该继续使用&#39;在循环中,因为我只想在键盘缓冲区中的当前数字已经在数组中的情况下保持位置不增加。我错过了什么?

3 个答案:

答案 0 :(得分:1)

Set<Integer> values = new HashSet<>();
    Scanner sc  = new Scanner(System.in);
    int count = 0;
    do {
        int in = sc.nextInt();
        if(in>0 && count < 20) {
            values.add(in);
        }else {
            break;
        }
    }while(sc.hasNext());

答案 1 :(得分:0)

尝试这样的事情;

int position = 0;
while (position < 20) {
   int next = keyboard.nextInt();
   if (next < 0) {
      break; // Negative so exit loop and return
   }

   if (!ArrayUtils.contains(set, next)) {
      set[position] = next;
      position++;
   }

return set;

答案 2 :(得分:0)

这是使用int数组和直接比较搜索的另一种解决方案。

import java.util.Scanner;

public class NumberScanner {

    private static final int MAX_SIZE = 20;

    static int[] intArray = new int[MAX_SIZE];

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        int value = 0;
        int size = 0; // Not an array counter, starts at 1. 0 means empty.

        System.out.println("Enter a series of positive integers on one or more lines. Enter a negative number to quit.");

        // Loop until a negative number is found or the MAX_SIZE is reached
        while (size < MAX_SIZE) {
            value = keyboard.nextInt();
            if (value < 0)
                break;

            boolean found = false;
            if (value >= 0 && size > 0) {
                // Add the value to the array
                // First, check if its already there
                for (int i = 0; i<size; i++) {
                    if (intArray[i] == value) {
                        found = true;
                        break;  // Stop the loop
                    }                   
                }
            }
            if (!found) {               
                intArray[size] = value;
                size++;
            }
        }

        for (int i=0; i<size; i++) {
            System.out.printf(intArray[i] + " ");
        }
        System.out.printf("\n");

        keyboard.close();
    }
}