taking user input array in java using scanner class [duplicate]

安稳与你 提交于 2019-12-06 17:35:02

问题


How to take input from user side for array using scanner class? Any other method will also be appreciated. for example, if we need to create an array and then taking input from user side. Thank you!!


回答1:


Check the following code...It is for reading integer array

    public class TakingInput {

    public static void main(String[] args) {

        Scanner s=new Scanner(System.in);

        System.out.println("enter number of elements");

        int n=s.nextInt();

        int arr[]=new int[n];

        System.out.println("enter elements");

        for(int i=0;i<n;i++){//for reading array
            arr[i]=s.nextInt();

        }

        for(int i: arr){ //for printing array

            System.out.println(i);

        }


    }

}


来源:https://stackoverflow.com/questions/25444786/taking-user-input-array-in-java-using-scanner-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!