为啥我不能输入我的号码?

Posted

技术标签:

【中文标题】为啥我不能输入我的号码?【英文标题】:Why can I not type in my numbers?为什么我不能输入我的号码? 【发布时间】:2015-10-30 17:42:56 【问题描述】:

大家好,所以每当我运行代码时,我都会要求我输入我的姓名、工作小时数和小时工资。这是正确的,但是当它进行计算时,程序使用默认构造函数中的值而不是我输入的值

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: ");
    

   

【问题讨论】:

请发布您的代码。 docs.google.com/document/d/… 请立即在文本编辑器中发布您的代码。 请仅在此处粘贴您的代码,而不是将其发布到谷歌驱动器/ 【参考方案1】:

你正在创建一个新的局部变量

double hours = Double.parseDouble(howmanyHours);

它隐藏了小时的类字段,并且没有在其他任何地方使用

【讨论】:

【参考方案2】:

您将名称、小时数和工资分配给在您的 main 方法中声明的变量,而不是属于您的 Payroll 对象的变量。例如:

double hours = Double.parseDouble(howmanyHours);

这应该是:

singh.hours = Double.parseDouble(howmanyHours);

您还需要以这种方式修复其他要分配给 singh 的变量。

【讨论】:

以上是关于为啥我不能输入我的号码?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我不能以此结束我的输入?

为啥我不能在我的 jQueryUI 模式对话框中输入 TinyMCE?

为啥我不能在我的 pygame 文本框中输入超过 1 个字母? [复制]

为啥我的命令提示符界面只能输入中文而不能输入英文!十分感谢!

为啥我的电脑按ctrl+shift不能切换输入法

为啥我的 if 函数没有从字符串中删除空格?它不能检测 C++ 中的空格输入吗?有没有办法从输入中删除空间