Arraylist - 字符串/双

0 java string double arraylist

我首先使用java类获取基本对象,我还不太了解并需要一些帮助..我需要将这些值分配给arraylist但还需要允许用户根据字符串选择一个健康选项然后输出与选项相关的值.

double [] healthBenDesig = new double [5];
double [] healthBenDesig = {0.00, 311.87, 592.56, 717.30, 882.60};
Run Code Online (Sandbox Code Playgroud)

我想分配的字符串是:

none = 0.00
employeeOnly = 311.87
spouse = 592.56
children = 717.30
kids = 882.60 
Run Code Online (Sandbox Code Playgroud)

最终,我希望用户输入例如"none",输出将none与arraylist [0]插槽中保存的值无关并返回该值.这可能吗?还是有一种我更容易忽视的方式?

如果有人能帮助我,我会非常感激:)谢谢

Ani*_*rni 6

是.这是可能的HashMap.

HashMap<String,Double> healthMap = new HashMap<String,Double>();
healthMap.put("none",0.00);
healthMap.put("employeeOnly",311.87);
healthMap.put("spouse",592.56);
healthMap.put("children",717.30);
healthMap.put("kids",882.60);
Run Code Online (Sandbox Code Playgroud)

现在,当用户输入none然后使用get()方法on healthMap获取值.

为了安全检查,使用containsKey()方法在地图中存在密钥.

if(healthMap.containsKey("none")) {
   Double healthVal = healthMap.get("none"); //it will return Double value
} else {
  //show you have entered wrong input
}
Run Code Online (Sandbox Code Playgroud)

也可以看看