简体   繁体   English

如何创建一组空数组并用用户输入填充并打印出来?

[英]How to create a empty set of array and fill it with user input and print it out?

Here is my code to fill an empty array : 这是我填充空数组的代码:

package duplicate.terminator;
import java.util.Arrays;
import java.util.Scanner;
public class DuplicateTerminator {
       public static void main(String args []){

           Scanner number = new Scanner(System.in);
           int a, num;
           int[] integerset = null;

                System.out.println("Enter Number: ");
                num = number.nextInt();
                Arrays.fill(integerset, num);

       }

}

That is my code because I want to have this output. 这是我的代码,因为我想要这个输出。 I need to stack input numbers in array and print it out like this. 我需要在数组中堆叠输入数字并将其打印出来。

Sample Input/Output:
    Enter number: 5
    5
    Enter number: 9
    5 9
    Enter number: 2
    5 9 2
    Enter number: 9
    9 has already been entered
    5 9 2
    Enter number: 1
    5 9 2 1

I would suggest using LinkedHashSet 我建议使用LinkedHashSet

because 因为

  • you don't know the number of elements in your data container initially 您最初不知道数据容器中的元素数量
  • you don't want duplicates and you don't want to go serially to check if there is a duplicate 你不想要重复,你不想连续检查是否有重复
  • you want to preserve order 你想保留秩序

Here is an example: 这是一个例子:

Set<Integer> numbers = new LinkedHashSet<>();

while(/*some logic to exit on special input*/) {
   if(!numbers.add(userInputNum){
     // number was already present
   }
}

If you don't want to use LinkedHashSet or fill method in array this is an alternate way to do it. 如果您不想在数组中使用LinkedHashSet或fill方法,则这是另一种方法。

public static void main(String[] args) 
{
    Scanner scan = new Scanner(System.in);
    int i, j;
    int[] list = new int[10];
    for (i = 0; i <= 9; i++)
    {
        System.out.println("Enter a number :");
        list[i] = scan.nextInt();
        for (j = 0; j < i; j++)
        {
            if (list[j] == list[i])
            {
                System.out.println(list[j] + " has already been entered");
                i--;
            }
        }
        for(j = 0; j <= i; j++)
        {
            System.out.print(list[j] + " ");
        }
        System.out.println();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从用户输入中打印一个单词 - How to print a single word out of a user input 如何从用户输入填充字符数组? - How to fill an array of characters from user input? 如何获取用户输入并将其存储到HashMap中并分别创建两个键以进行打印? - How do I get user input and store it into a HashMap and create the two keys separately to print out? (Java) 如何根据用户输入创建一个大小的数组,并用随机数填充它 - (Java) How to create an array with size based on user input, and also fill it with random numbers 使用2D数组和用户输入创建和填充网格 - Create and fill grid using 2D array and user input 如何在 Java 中的数组中收集用户输入,然后使用类打印出来 - How do I collect user input inside an array in Java and print then out using classes 获取用户输入,放入数组并打印出每个字母的使用次数 - Take user input, put into an array and print out how many times each letter is used 根据用户输入从数组中打印出某些字符 - Print out certain characters from array based on user input 如果要求用户为数组提供输入,而没有输入,那么如何将元素设置为0? - If a user is required to provide an input to an array, if he leaves out an input, how does one set that element to 0? 设置数组大小并将用户输入的字符串解析为数组 - Set Array size and parse out String user input into Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM