gpt4 book ai didi

java - 这个 HashMap 有什么问题,我不断收到不兼容的类型

转载 作者:行者123 更新时间:2023-12-01 18:17:23 25 4
gpt4 key购买 nike

//vehicle
double type[] = {0, 1.0, 1.3, 1.6, 2, 2.4, 2.7};
String carType[] = {"","Compact Car", "Small Car", "Mid Size Car", "Full Size Car", "Truck", "16 Wheeler"};
HashMap <Integer, Double> vehicle = new HashMap<Integer, Double>();
for(int y = 1; y<6; y++)
{
vehicle.put(y, type[y]);
}
HashMap <Integer, String> CarName = new HashMap<>();
for(int a = 1; a<6; a++)
{
CarName.put(a, carType[a]);
}

/**
* Now begin to read the file and reference the tables.
*/
int gate1, vehicle1, toll1, factor, cost;
String car;
System.out.println(EasyFormat.format("Car Type", 0)+EasyFormat.format("Base Toll", 15)+EasyFormat.format("Factor", 22)+EasyFormat.format("Cost",30));
while(file.hasNext())
{
gate1 = file.nextInt();
vehicle1 = file.nextInt();
factor = (Double) vehicle.get(vehicle1);
}

最佳答案

这里:

int factor; //other variables as well
//more code...
while(file.hasNext()) {
//more code...
factor = (Double) vehicle.get(vehicle1);
}

factorint 类型,并且您尝试为其分配 Double

factor声明为double或使用Double#intValue。我更愿意将 factor 声明为 double 并让拆箱完成其工作。

关于java - 这个 HashMap 有什么问题,我不断收到不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28835821/

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