gpt4 book ai didi

java - 变量可能尚未初始化?

转载 作者:行者123 更新时间:2023-11-30 04:58:00 24 4
gpt4 key购买 nike

我正在尝试将“bonusStr”变量转换为 double 型,以便可以在计算中使用它。然而,当尝试编译时,我收到错误“变量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/

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