今天我需要一些帮助,将 4 个用户定义的字符串、2 个 int 和 4 个 double 变量作为一个索引添加到数组列表中。
例如:(我会为您减少一些变量,请记住 sc 是我的扫描仪对象,测试结果满分 100)
System.out.print("enter your name")
String name = sc.next();
System.out.print("enter your test results")
double results = sc.nextDouble();
System.out.print("enter your mobile number")
int mobile_num = sc.nextInt(); //I may change this to a string
现在,我想捕获这些输入并将它们存储在数组中的数字 1 或用户名下。我还想知道如何使用同一个数组通过上述代码存储用户的无限输入。
提前致谢!
最佳答案
创建您自己的类 UserInput
它保留 10 个字段,然后概括一个 List
使用这种类型:
List<UserInput> list = new ArrayList<>();
list.add(new UserInput(name, results, mobile_num, ...));
如果你想将用户名与其相应的输入关联起来,你最好考虑 Map<String, UserInput>
:
Map<String, UserInput> map = new HashMap<>();
map.put(name, new UserInput(results, mobile_num, ...));
关于java - 如何将多个变量作为单个值添加到数组列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41315141/