大家好,所以每当我运行代码时,我都会要求我输入自己的姓名、工作小时数和时薪。这是正确的,但是当它进行计算时,程序使用默认构造函数中的值,而不是我在中键入的值
import javax.swing.JOptionPane;
public class Payroll
{
private double rate;
private double hours;
private String name;
static double grossPay=0;
static double unionDues=0;
static double medicalCoverage=0;
static double ss=0;
static double medicare=0;
static double state=0;
static double federal=0;
static double netPay=0;
static int numPeople=0;
public Payroll()
{
rate = 0;
hours = 0;
name = "Manmeet Singh";
}
public void setName(String newName)
{
name=newName;
}
public void setHours(double newHours)
{
hours=newHours;
}
public void setRate(double newRate)
{
rate=newRate;
}
public void calcPay()
{
double ot=hours-40;
double otRate=rate*1.5;
double mediPay=34.61;
double uniPay=15.25;
double otgrossPay=((40*rate)+(otRate*ot));
double noOtPay=(hours*rate);
double otgrossIncome=((otgrossPay)-(34.61+15.25));
double grossIncome=((noOtPay)-(34.61+15.25));
if (hours>40)
{
JOptionPane.showMessageDialog(null,"Gross Pay for " +name +" is: $"+otgrossPay);
JOptionPane.showMessageDialog(null,"Health Deduction is: $"+mediPay);
JOptionPane.showMessageDialog(null,"Union Deduction is: $"+uniPay);
if (otgrossIncome>=0 && otgrossIncome<=150)
{
System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.07));
System.out.println("State Tax Deduction is: $"+(otgrossIncome*.002));
System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.002)+(otgrossIncome*.07))));
}
else if (otgrossIncome>=150.01 && otgrossIncome<=300)
{
System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.098));
System.out.println("State Tax Deduction is: $"+(otgrossIncome*.021));
System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.021)+(otgrossIncome*.098))));
}
else if (otgrossIncome>=300.01 && otgrossIncome<=600)
{
System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.124));
System.out.println("State Tax Deduction is: $"+(otgrossIncome*.0386));
System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.0386)+(otgrossIncome*.124))));
}
else if (otgrossIncome>=600)
{
System.out.println("Federal Tax Deduction is: $"+(otgrossIncome*.1887));
System.out.println("State Tax Deduction is: $"+(otgrossIncome*.055));
System.out.println("Social Security Deduction is: $"+(otgrossIncome*.062));
System.out.println("Medicare Deduction is: $"+(otgrossIncome*.0145));
System.out.println("Net Pay is: $"+(otgrossIncome-((otgrossIncome*.0145)+(otgrossIncome*.062)+(otgrossIncome*.055)+(otgrossIncome*.1887))));
}
grossPay=otgrossPay+grossPay;
unionDues=unionDues+unionDues;
medicalCoverage=medicalCoverage+medicalCoverage;
ss=0;
medicare=0;
state=0;
federal=0;
netPay=0;
numPeople=0;
}
else if (hours<=40)
{
System.out.println("Gross Pay for " +name + " is: $"+noOtPay);
System.out.println("Health Deduction is: $"+mediPay);
System.out.println("Union Deduction is: $"+uniPay);
if (grossIncome>=0 && grossIncome<=150)
{
System.out.println("Federal Tax Deduction is: $"+(grossIncome*.07));
System.out.println("State Tax Deduction is: $"+(grossIncome*.002));
System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.002)+(grossIncome*.07))));
}
else if (grossIncome>=150.01 && grossIncome<=300)
{
System.out.println("Federal Tax Deduction is: $"+(grossIncome*.098));
System.out.println("State Tax Deduction is: $"+(grossIncome*.021));
System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.021)+(grossIncome*.098))));
}
else if (grossIncome>=300.01 && grossIncome<=600)
{
System.out.println("Federal Tax Deduction is: $"+(grossIncome*.124));
System.out.println("State Tax Deduction is: $"+(grossIncome*.0386));
System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.0386)+(grossIncome*.124))));
}
else if (grossIncome>=600)
{
System.out.println("Federal Tax Deduction is: $"+(grossIncome*.1887));
System.out.println("State Tax Deduction is: $"+(grossIncome*.055));
System.out.println("Social Security Deduction is: $"+(grossIncome*.062));
System.out.println("Medicare Deduction is: $"+(grossIncome*.0145));
System.out.println("Net Pay is: $"+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.055)+(grossIncome*.1887))));
}
grossPay=noOtPay+grossPay;
unionDues=unionDues+uniPay;
medicalCoverage=mediPay+medicalCoverage;
ss=0;
medicare=0;
state=0;
federal=0;
netPay=0;
numPeople=0;
}
else
{
System.out.println("You entered a wrong value");
}
}
public static void main (String [] args)
{
boolean y=true;
Payroll singh = new Payroll();
while(y==true)
{
String employeeName=JOptionPane.showInputDialog("Please enter the name");
String howmanyHours=JOptionPane.showInputDialog("Please enter the number of hours worked ");
double hours = Double.parseDouble(howmanyHours);
String whatWages= JOptionPane.showInputDialog("Please enter the hourly wage ");
double rate = Double.parseDouble(whatWages);
if (rate<8.75)
{
JOptionPane.showMessageDialog(null," Oh No!! n You are not being paid the New York State minimum n Try Again");
}
singh.calcPay();
String x = JOptionPane.showInputDialog("Are there more employees?n Type y below");
if (x.equals("y") || x.equals("Y"))
{
y = true;
}
else
{
y = false;
System.out.println("Summary of totals: ");
System.out.println("Number of employees on the payroll: ");
System.out.println("Total Health: "+medicalCoverage);
System.out.println("Total Union Dues: "+unionDues);
System.out.println("Total Federal Taxes: ");
System.out.println("Total State Taxes: ");
System.out.println("Total Social Security Taxes: ");
System.out.println("Total Medicare Taxes: ");
}
}
}}
您正在创建一个新的本地变量
double hours = Double.parseDouble(howmanyHours);
它隐藏了小时的类字段,在其他任何地方都不使用
您将名称、小时数和工资分配给在主方法中声明的变量,而不是属于Payroll
对象的变量。例如:
double hours = Double.parseDouble(howmanyHours);
这应该是:
singh.hours = Double.parseDouble(howmanyHours);
您还需要以这种方式修复要分配给singh
的其他变量。