java - 用户输入的数组大小和元素
问题描述
我想要一个程序,它首先询问数组的大小,然后询问元素。数组的大小是 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
我的问题是,如何询问尺寸和元素?提前致谢!
解决方案
使用下面的代码。
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();
}
推荐阅读
- events - Outlook 插件:在插件代码(js 文件)中接收事件
- arrays - 我需要从数组中减去两个时间戳的总和
- r - 如何为命名列表中的名称分配变量值?
- python - 在 3d 空间中连接两个 12 多边形
- python - 如何在 python 中验证来自 AzureAD 的 JWT?
- php - 在 PHP 上设置 CORS 以允许某些域
- vue.js - 如何使用 Vee validate 验证 vue-bootstrap-typeahead?
- c++ - 如何制作一个采用引用而不是副本的可变参数模板方法?
- asp.net-mvc - 绑定到模型时如何设置默认选择的组合框?
- c# - 如何在 IIS 中使用 WebApi 2 c# 托管 Angular 应用程序?