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

问题描述 投票:-3回答:1


//请错过


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();
      }

}
java
1个回答
-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();
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.