使用扫描仪类在java中获取用户输入数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25444786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:25:13 来源:igfitidea点击:
taking user input array in java using scanner class
提问by Roushan J.
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!!
如何使用扫描仪类从用户端获取数组输入?任何其他方法也将受到赞赏。例如,如果我们需要创建一个数组,然后从用户端获取输入。谢谢!!
回答by SureshBonam
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);
}
}
}