gpt4 book ai didi

java - 使用 “new double”时发生编译时间错误

转载 作者:行者123 更新时间:2023-12-02 11:07:28 26 4
gpt4 key购买 nike

我已经创建了一个哈希表,并且试图使用枚举来打印键和值。当我尝试编译代码时,我不断收到编译时错误消息,提示我需要在散列表中放入新 double 数的'['。

编译前:
toys.put(“Race Car”,new double(29.99));

编译时错误说我需要这样放置:

toys.put(“Race Car”,new double [29.99]);

我究竟做错了什么?

public static void main(String[] args)  {
Hashtable toys = new Hashtable();
Enumeration toyName;
String getToyName;
double priceOfToy;
toys.put("Race Car", new double(29.99));
toys.put("Toy Bear", new double(15.99));
toys.put("Action Figure", new double(9.99));
//Show prices of each toy
toyName = toys.keys();
//Uses hasMoreElements method from enumeration to check what is in the hashtable
while (toyName.hasMoreElements()) {
//uses nextElement method from enumeration interface to
getToyName = (String) toyName.nextElement();
System.out.println(getToyName + "is now priced at " + toys.get(getToyName));
}

}

最佳答案

Map仅接受对象,不接受基元,double是基元,而Double是对象。
并考虑为您的集合使用通用类型:

Hashtable<String, Double> toys = new Hashtable<String, Double>();     
toys.put("Race Car", new Double(29.99));
toys.put("Toy Bear", new Double(15.99));
toys.put("Action Figure", new Double(9.99));}

关于java - 使用 “new double”时发生编译时间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com