用户输入的数组大小和元素

时间:2019-02-28 08:36:55

标签: java arrays

我想要一个程序,该程序首先询问数组的大小,然后询问元素。数组的大小为int,存储的值为double。像这样:

How many numbers? 5 // 5 is user input
Please type the numbers:
1,111
4
11,45
21
3
The numbers in reverse order are:
3.0 21.0 11.45 4.0 1.111

我的问题是,如何要求尺寸和元素?预先感谢!

1 个答案:

答案 0 :(得分:1)

使用以下代码。

Scanner sc = new Scanner(System.in); 
int arraysize=sc.nextInt();
double[] doubleArray = new double[arraysize];

for(int i=0; i<arraysize; i++){
doubleArray[i] = sc.nextDouble(); 

}