Java 为什么我不能输入我的号码?
大家好,每当我运行代码时,我都会要求我输入我的姓名、工作小时数和小时工资。这是正确的,但是当它进行计算时,程序使用默认构造函数中的值,而不是我键入的值Java 为什么我不能输入我的号码?,java,modifiers,Java,Modifiers,大家好,每当我运行代码时,我都会要求我输入我的姓名、工作小时数和小时工资。这是正确的,但是当它进行计算时,程序使用默认构造函数中的值,而不是我键入的值 import javax.swing.JOptionPane; public class Payroll { private double rate; private double hours; private String name; static double grossPay=0; static double unionDues=0; st
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: ");
}
}
import javax.swing.JOptionPane;
公共类工资单
{
私人双费率;
私人双小时;
私有字符串名称;
静态双格罗斯比=0;
静态双银联=0;
静态双医疗平均值=0;
静态双ss=0;
静态双重医疗保险=0;
静态双态=0;
静态双联邦=0;
静态双网支付=0;
静态int numpople=0;
公共薪金总额()
{
比率=0;
小时=0;
name=“Manmeet Singh”;
}
public void setName(字符串newName)
{
name=newName;
}
公共无效设置小时数(双倍新小时数)
{
小时=新小时;
}
公共无效设置率(双倍新利率)
{
利率=新利率;
}
公共无效calcPay()
{
双ot=小时-40;
双三分之一=比率*1.5;
双倍mediPay=34.61;
双倍统一支付=15.25;
双倍总收入=((40倍税率)+(三倍税率)+);
双休日=(小时*费率);
双倍奥特格罗斯科姆=((奥特格罗斯帕伊)-(34.61+15.25));
双倍总收入=((noOtPay)-(34.61+15.25));
如果(小时数>40)
{
showMessageDialog(null,“为“+name+”支付的总金额为:$”+Otgrospay);
showMessageDialog(null,“健康扣减为:$”+mediPay);
showMessageDialog(null,“工会扣减额为:$”+uniPay);
如果(otgrossIncome>=0&&otgrossIncome=150.01&&otgrossIncome=300.01&&otgrossIncome=600)
{
System.out.println(“联邦税收扣减额为:$”+(otgrossIncome*.1887));
System.out.println(“州税扣减额为:$”+(otgrossIncome*.055));
System.out.println(“社会保障扣除额为:$”+(otgrossIncome*.062));
System.out.println(“医疗保险扣除额为:$”+(otgrossIncome*.0145));
System.out.println(“净薪酬为:$”+(奥特格罗辛科姆-((奥特格罗辛科姆*.0145)+(奥特格罗辛科姆*.062)+(奥特格罗辛科姆*.055)+(奥特格罗辛科姆*.1887));
}
格罗斯佩=奥特格罗斯佩+格罗斯佩;
银联会费=银联会费+银联会费;
医疗平均=医疗平均+医疗平均;
ss=0;
医疗保险=0;
状态=0;
联邦=0;
净支付=0;
numPeople=0;
}
否则如果(小时数=0&&grossIncome=150.01&&grossIncome=300.01&&grossIncome=600)
{
System.out.println(“联邦税收扣减额为:$”+(grossIncome*.1887));
System.out.println(“州税扣减额为:$”+(grossIncome*.055));
System.out.println(“社会保障扣除额为:$”+(grossIncome*.062));
System.out.println(“医疗保险扣除额为:$”+(grossIncome*.0145));
System.out.println(“净薪酬为:$”+(grossIncome-((grossIncome*.0145)+(grossIncome*.062)+(grossIncome*.055)+(grossIncome*.1887));
}
grossPay=noOtPay+grossPay;
银联会费=银联会费+统一支付;
医疗平均=医疗支付+医疗平均;
ss=0;
医疗保险=0;
状态=0;
联邦=0;
净支付=0;
numPeople=0;
}
其他的
{
System.out.println(“您输入了错误的值”);
}
}
公共静态void main(字符串[]args)
{
布尔y=真;
Payroll singh=新工资单();
while(y==true)
{
字符串employeeName=JOptionPane.showInputDialog(“请输入名称”);
字符串howmanyHours=JOptionPane.showInputDialog(“请输入工作小时数”);
双倍小时=double.parseDouble(多少小时);
字符串whatWages=JOptionPane.showInputDialog(“请输入小时工资”);
双速率=double.parseDouble(whatWages);
如果(rate您正在创建一个新的局部变量
double hours = Double.parseDouble(howmanyHours);
它隐藏了小时的类字段,不在其他任何地方使用您正在将名称、小时数和工资分配给main方法中声明的变量,而不是属于您的Payroll
对象的变量。例如:
double hours = Double.parseDouble(howmanyHours);
这应该是:
singh.hours = Double.parseDouble(howmanyHours);
您还需要通过这种方式修复分配给singh的其他变量。请发布您的代码。请立即在文本编辑器中发布您的代码。请仅在此处粘贴您的代码,而不要将其发布到google drive/