Valriable 可能尚未初始化?

发布于 2024-12-12 01:54:41 字数 1270 浏览 0 评论 0原文

我正在尝试将“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

仙女山的月亮 2024-12-19 01:54:41
String bonusStr;
double bonus = Double.parseDouble(bonusStr);

由于错误表明 bonusStr 未初始化(您没有影响它的值),因此您不应该在 Double.parseDouble 内使用它,直到它有一个值。

String bonusStr;
double bonus = Double.parseDouble(bonusStr);

As the error states bonusStr is not initialized (you did not affect a value to it), so you should not use it inside Double.parseDouble until it has a value.

靑春怀旧 2024-12-19 01:54:41
   String bonusStr;
          double bonus = Double.parseDouble(bonusStr);

您永远不会为 bonusStr 设置值 - 默认情况下它将为 null。您在赋予它值之前就使用了它。尝试:

String bonusStr = "0";

给它一个默认值是个好主意,比如 0 或其他值可以帮助您诊断您是否忘记提供正确的值。

   String bonusStr;
          double bonus = Double.parseDouble(bonusStr);

You never set a value to bonusStr - by default it is going to be null. You are using it before giving it a value. Try :

String bonusStr = "0";

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.

大姐,你呐 2024-12-19 01:54:41

您的程序将引发 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文