【发布时间】:2014-10-25 02:10:44
【问题描述】:
这是我目前拥有的代码。
我需要将最后一个 if 语句转换为双精度
public static void main(String[] args) {
double skill = 0; double hours = 0; double overTime= 0.5; int insurance =0;
String[] choices = {"Medical Insurance", "Dental Insurance", "Long-Term Disability Insurance"};
double MedicalInsuranceCount = 32.50;
double DentalInsuranceCount = 20.00;
double LongTermDisabilityInsuranceCount = 10.00
if (skill >= 20.00 && skill <=22.00)
insurance = JOptionPane.showOptionDialog(
null // Center in window.// prompts the user to select a button
, "Types of Insurance options" // Message
, "Insurance option" // Title in titlebar
, JOptionPane.YES_NO_OPTION // Option type
, JOptionPane.PLAIN_MESSAGE // messageType
, null // Icon (none)
, choices // Button text as above.
, "None of your business" // Default button's label
);
switch (insurance)
{
case 0:
MedicalInsuranceCount++;
break;
case 1:
DentalInsuranceCount++;
break;
case 2:
LongTermDisabilityInsuranceCount++;
break;
default:
//... If we get here, something is wrong. Defensive programming.
JOptionPane.showMessageDialog(null, "Unexpected response " + insurance);
}
// this is where the problem is I think.
if(insurance == 0)
insurance = Math.round((float)MedicalInsuranceCount);
else if(insurance == 1)
insurance = Math.round((float)DentalInsuranceCount);
else if(insurance == 2)
insurance = Math.round((float)LongTermDisabilityInsuranceCount);
System.out.println(insurance);
}
}
我尝试使用 Double.PasreDouble 但失败了,Math.round(float) 是唯一有效的方法
【问题讨论】:
-
它输出了什么,你期待什么,如果它有效,
Math.round(float)有什么问题? -
您将
insurance变量用于多种用途。首先你需要解决这个问题,然后我们可以帮助你(如果仍然需要) -
你的第一个 if 块将永远运行,因为当 if 块出现时,技能将始终为 0。