我应该编写的代码是一个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.");
}
}
答案 0 :(得分:2)
在这个if语句中,如果hoursWorked
不大于40,则可以跳过为overtimeHours,overtimePay和regularPay变量赋值。在代码的最后,你尝试{{1显示这些变量。
showMessageDialog
你现在明白为什么它会给你这个警告吗?在某个代码路径上,您尝试打印出尚未初始化的变量值。
答案 1 :(得分: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
中使用它们时,会得到这些变量可能尚未初始化的错误。