简体   繁体   English

Java - 使用户输入到数组中

[英]Java - making user input into arrays

So I am super new in terms of coding and java, I just started this week but am already stuck at a pretty simple problem.所以我在编码和 Java 方面是超级新手,我这周刚开始,但已经陷入了一个非常简单的问题。 I am supposed to first check that there are only five integers that the user inputs (on the same line) - which I have tried to do and maybe succeeded?我应该首先检查用户输入的只有五个整数(在同一行) - 我已经尝试过并且可能成功了?

But then, if the user does input five integers, I am supposed to make that input into an array with integers and I just have no idea how to do that.但是,如果用户确实输入了五个整数,我应该将该输入放入一个带有整数的数组中,而我只是不知道该怎么做。 I tried to figure it out on my own but I simply didn't understand what to do.我试图自己弄清楚,但我根本不知道该怎么做。 Could anyone explain this to me, I'd be very grateful!谁能给我解释一下,我将不胜感激!

Here is my (probably very lacking) code:这是我的(可能非常缺乏)代码:

import java.util.Scanner;

public class Try2 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inputR = in.nextLine(); 
        int lengthArgs = 5;
        int nummer = (inputR.split(" ").length);    
        if(nummer == lengthArgs) {
         //???
        }
        else {
          System.out.println("Wrong");
        }
    }
}

You are already splitting the input into an array, but not retaining it.您已经将输入拆分为一个数组,但没有保留它。 Try this:尝试这个:

  Scanner in = new Scanner(System.in);
    String inputR = in.nextLine(); 
    int lengthArgs = 5;
    String[] values inputR.split(" ");

    if(values.length == lengthArgs) {
        int[] numbers = new int[values.length];
        for(int i = 0;i < lengthArgs;i++) {
          // Note that this is assuming valid input
          // If you want to check then add a try/catch 
          numbers[i] = Integer.parseInt(values [i]);
       }
   }else {
   System.out.println("Wrong");

Another idea, not so simple as array, but more elegant is with using Collection of Integers, like this:另一个想法,不像数组那么简单,但更优雅的是使用整数集合,如下所示:

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    String inputR = in.nextLine();
    String inputs[] = inputR.split(" ");

    if(inputs.length != 5) {
        System.out.println("Invalid number of input");
    }

    List<Integer> integers = new ArrayList<>();
    for (String s : inputs) {
        Integer number = Integer.parseInt(s);
        integers.add(number);
    }
    System.out.println("Got following numbers in my input: "  + integers);
}

You can just do this.你可以这样做。

 Scanner in  = new Scanner(System.in);
     int arr[] = new int [5];
      for(int i = 0; i < 5; i++){
       int a = in.nextInt();
       arr[i] = a;
       }

      for(int i : arr)
          System.out.print(i + " ");
import java.util.Arrays;
import java.util.Scanner;

public class Test {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int lengthArgs = 5;

    String[] enteredChars;
    do {
        System.out.println("enter " + lengthArgs + "  numbers");
        enteredChars = in.nextLine().split(" ");
    } while (enteredChars.length != lengthArgs);

    int[] array = new int[lengthArgs];
    for (int i = 0; i < lengthArgs; i++) {
        try {
            array[i] = Integer.parseInt(enteredChars[i]);
        } catch (NumberFormatException e) {
            e.printStackTrace();// You have to do make sure that than you again have to enter all x numbers
        }
    }

    System.out.println("Your array " + Arrays.toString(array));
}
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM