【问题标题】:Array size and elements by user input [closed]用户输入的数组大小和元素[关闭]
【发布时间】:2019-02-28 08:36:54
【问题描述】:

我想要一个程序,它首先询问数组的大小,然后询问元素。数组的大小是 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

我的问题是,如何询问尺寸和元素?提前致谢!

【问题讨论】:

  • System.in.read() 是你的朋友
  • 你也可以看看 Scanner 类。虽然 System.in.read() 也可以
  • 扫描仪 sc = new Scanner(System.in); int size=sc.nextInt();
  • 在某些系统上还支持Console console = System.console();,让您的生活更轻松

标签: java arrays


【解决方案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(); 

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多