变量可能未初始化

时间:2015-03-14 15:39:29

标签: java

我试图弄清楚我的代码是什么问题。当我编译它时,变量checkfee可能尚未初始化。

import javax.swing.JOptionPane;

public class BankCharge  {
    public static void main (String [] args) {
        int check, basefee;
        String temp;
        double checkfee; 
        double totalfee;
        basefee = 10;
        totalfee = checkfee + basefee;


        temp = JOptionPane.showInputDialog("Enter the number of checks");
        check = Integer.parseInt(temp);

        if (check < 20) {
            checkfee = .10 * check;
            JOptionPane.showMessageDialog(null, "Your amount for the month is " + totalfee);
        }
        if (check >= 20 && check <= 39) {
            checkfee = .08 * check;
            JOptionPane.showMessageDialog(null, "Your amount for the month is " + totalfee);
        }
        if (check >= 40 && check <= 59) {
            checkfee = .06 * check;
            JOptionPane.showMessageDialog(null, "Your amount for the month is " + totalfee);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

因为checkfee变量的初始化总是在条件语句(if)中完成,所以不能保证初始化。

答案 1 :(得分:0)

此警告并不重要。您可以编写double checkfee=0;来删除它。如果选中&gt; 60 ??

,你会怎么做?