如何通过Scanner将数字添加到数组中

时间:2018-03-05 05:01:56

标签: java

//请错过

public static void mian(String[] args){
      Scanner scanner = new Scanner(System.in);

     int [] array = null;
      for(int i = 0;i <n;i++){
            int array[i]  =  scanner.nextInt();
      }

}

1 个答案:

答案 0 :(得分:-1)

import java.util.Scanner;

public class MyMain {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int [] array = new int[5];
          for(int i = 0;i < 5;i++){
              System.out.println("Enter number ");
                array[i]  =  scanner.nextInt();
          }

          for(int i : array) {
              System.out.println(i);
          }

          scanner.close();
    }

}