//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);
}
factor
是 int
类型,并且您尝试为其分配 Double
。
将factor
声明为double
或使用Double#intValue
。我更愿意将 factor
声明为 double
并让拆箱完成其工作。
关于java - 这个 HashMap 有什么问题,我不断收到不兼容的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28835821/