0

私が書くことになっているコードは、ユーザーの入力を収集し、給与情報を出力する Payroll プログラムです。プログラムを作成しましたが、「ローカル変数が初期化されていない可能性があります。

import javax.swing.JOptionPane;



public class Payroll 
{
public static void main(String []args)
{

    String name, rateStr, hoursStr, maritalStatus, deductionStr, numEmpStr;
    double numEmp, rateOfPay, hoursWorked, regularPay, overtimeHours,     overtimePay, insuranceRate, taxRate, grossPay, taxes, deduction, netPay;  


    final double taxRate1 = .0857;
    final double taxRate2 = .0756;
    final double taxRate3 = .0579;


    numEmpStr = JOptionPane.showInputDialog("How many employees do you have? ");
    numEmp = Double.parseDouble(numEmpStr);

    while(numEmp >0 )
    {

        //Input All User Info!
        name = JOptionPane.showInputDialog("Employee Name: ");

        rateStr = JOptionPane.showInputDialog("Rate of Pay: ");
        rateOfPay = Double.parseDouble(rateStr);

        hoursStr = JOptionPane.showInputDialog("Hours Worked: ");
        hoursWorked = Double.parseDouble(hoursStr);

        deductionStr = JOptionPane.showInputDialog("Number of Deductions (Enter 1 or 2): ");
        deduction = Double.parseDouble(deductionStr);

        maritalStatus = JOptionPane.showInputDialog("Marital Status (M - Married, S - Single): ");

        //if statement (if hoursWorked >40


        if(hoursWorked> 40)
        {
        overtimeHours = (hoursWorked - 40);
        overtimePay = (rateOfPay*1.5*overtimeHours);

        regularPay = (40*rateOfPay);

        grossPay = (regularPay + overtimePay);
        }
        else
        {
            grossPay = (rateOfPay*hoursWorked);
        }



        if(maritalStatus == "M" )
        {
            insuranceRate =  50;
        }
        else
        {
            insuranceRate = 75;
        }



        if(deduction>2)
        {
            taxRate = .0579;
        }
        else if(deduction==2)
        {
            taxRate = .0759;
        }
        else if(deduction==1)
        {
            taxRate = .0857;
        }
        else
        {

        }


        taxes = (grossPay*taxRate);
        netPay = (grossPay - (taxes + insuranceRate));


        JOptionPane.showMessageDialog(null, "Name: " + name +
                                            "\nRate of Pay: " + rateOfPay +
                                            "\nHours Worked: " + hoursWorked +
                                            "\nRegular Pay: " + regularPay +
                                            "\nOvertime Pay: " + overtimePay +
                                            "\nGross Pay: " + grossPay +
                                            "\nTax Rate: " + taxRate +
                                            "\nTaxes: " + taxes +
                                            "\nInsurance Status: " + maritalStatus + 
                                            "\nDeductions: " + insuranceRate +
                                            "\nNet Pay: " + netPay);


        --numEmp;
    }

    JOptionPane.showMessageDialog(null, "Your are finished with payroll for this period.");
}

}
4

2 に答える 2

2

この if ステートメントでは、overtimeHours、overtimePay、regularPay 変数への値の割り当てを省略できます (値hoursWorkedが 40 以下の場合)。コードの最後で、showMessageDialogこれらの変数を表示しようとします。

    if(hoursWorked> 40)
    {
    overtimeHours = (hoursWorked - 40);
    overtimePay = (rateOfPay*1.5*overtimeHours);

    regularPay = (40*rateOfPay);

    grossPay = (regularPay + overtimePay);
    }
    else
    {
        grossPay = (rateOfPay*hoursWorked);
    }

この警告が表示された理由がわかりましたか? 特定のコード パスで、まだ初期化されていない変数の値を出力しようとしています。

于 2013-10-15T05:42:58.840 に答える
1
double numEmp, rateOfPay, hoursWorked, regularPay, overtimeHours,     overtimePay, insuranceRate, taxRate, grossPay, taxes, deduction, netPay;
overtimePay = 0.0;
taxRate = 0.0;
regularPay = 0.0;

このように初期化できます。

これを行う必要があるのは、これらの変数がすべて使用され、おそらく条件ステートメント (この場合はif) で何らかの値が割り当てられるためです。

if (hoursWorked > 40) {
    overtimeHours = (hoursWorked - 40);
    overtimePay = (rateOfPay * 1.5 * overtimeHours); // Conditional assignment, as its not assigned by default nor in the else

    regularPay = (40 * rateOfPay); // Conditional assignment, as its not assigned by default nor in the else

    grossPay = (regularPay + overtimePay); // This won't be a problem as its assigned some value in else part too.
}else {
    grossPay = (rateOfPay*hoursWorked);
}

そのため、 でそれらを使用しようとするとJOptionPane.showMessageDialog、これらの変数が初期化されていない可能性があるというエラーが表示されます。

于 2013-10-15T05:42:48.353 に答える