我试图将“bonusStr”变量转换为双精度型,因此可以用于计算。但是,当试图编译时,我得到错误“变量bonusStr可能未被初始化”。我知道这是一个非常新鲜的问题,但任何帮助将不胜感激。Valriable可能没有被初始化?
非常感谢!
在几分钟内没有预料到会有这么多回复 - 我已经解决了这个问题。谢谢你们。 :-)
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;
}
}