我正在尝试将“ bonusStr”变量转换为双精度,因此可以在计算中使用它。但是,当尝试编译时,出现错误“变量bonusStr可能尚未初始化”。我知道这是一个真正的新手问题,但是您所提供的任何帮助将不胜感激。
非常感谢!
几分钟后就不会收到这么多答复-我已经解决了这个问题。谢谢你们。 :-)
import static javax.swing.JOptionPane.*;
import java.text.DecimalFormat;
class Question3 {
public static void main(String[] args) {
String intrestRateStr = showInputDialog("What is the interest rate?");
int intrestRate = Integer.parseInt(intrestRateStr);
String depositStr = showInputDialog("How much will you deposit?");
double depositAmount = Double.parseDouble(depositStr);
DecimalFormat pounds = new DecimalFormat("£###,##0.00");
double amountInterest = calcAmount(intrestRate, depositAmount);
String bonusStr;
double bonus = Double.parseDouble(bonusStr);
if (amountInterest >= 5000.00)
bonus = (+100.00);
else if (amountInterest >= 1000.00)
bonus = (+50.00);
double finalAmountInterestBonus = bonus + amountInterest;
showMessageDialog(null,
"Your savings will become " + pounds.format(finalAmountInterestBonus));
}
private static double calcAmount(int intRate, double depAmount) {
double result = depAmount*(1.0 + intRate/100.0);
return result;
}
}
最佳答案
String bonusStr;
double bonus = Double.parseDouble(bonusStr);
由于错误状态
bonusStr
尚未初始化(您没有影响它的值),因此,在它具有值之前,请勿在Double.parseDouble
中使用它。关于java - 变量可能尚未初始化?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7903923/