Valriable 可能尚未初始化?
我正在尝试将“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;
}
}
I'm trying to convert the "bonusStr" variable into a double, so it can be used in the calculation. However when trying to compile I get the error "variable bonusStr might not have been initialized". I know that this is a really newbie question, but any help given will be appreciated.
Many thanks!
Wasn't expecting so many responses in a few minutes - I've resolved the issue. Thank you to all. :-)
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于错误表明
bonusStr
未初始化(您没有影响它的值),因此您不应该在Double.parseDouble
内使用它,直到它有一个值。As the error states
bonusStr
is not initialized (you did not affect a value to it), so you should not use it insideDouble.parseDouble
until it has a value.您永远不会为
bonusStr
设置值 - 默认情况下它将为null
。您在赋予它值之前就使用了它。尝试:给它一个默认值是个好主意,比如 0 或其他值可以帮助您诊断您是否忘记提供正确的值。
You never set a value to
bonusStr
- by default it is going to benull
. You are using it before giving it a value. Try :It's a good idea to give it a default value, say 0 or something can help you diagnose that you have forgotten to give a proper value.
您的程序将引发 NumberFormatException。您对没有值的字符串执行了 parseDouble。在对它执行 parseDouble 之前,您必须先为bonusStr 设置一个值。
Your program will raise a NumberFormatException. You do a parseDouble of a string that has no value. You have to set the bonusStr with a value before doing a parseDouble on it.