-1

所以当我运行它时,它给了我总工资,但当我这样做时它只有 0.0 ......我需要知道如何让它显示实际输出。

import javax.swing.JOptionPane;


public class Payroll_demo

{
   public static void main(String[] args)

    {

    String input;

    Payroll A = new Payroll();

    input = JOptionPane.showInputDialog(null, "What is your name?");

    A.setName(input);

    input = JOptionPane.showInputDialog(null, "What is your ID number?");

    int input_b = Integer.parseInt(input);

    A.setID(input_b);

    input = JOptionPane.showInputDialog(null, "What is your hourly pay rate?");

    A.setPayRate(input);

    input = JOptionPane.showInputDialog(null, "How many hours did you work?");

    A.setHours(input);

    JOptionPane.showMessageDialog(null, A.Gross());


  }


}

我认为这可能是构造函数问题,但老实说我不知道​​。我觉得好像我几乎尝试了所有东西,但必须有一些东西......

public class Payroll

{

private String name;

private int ID;

private double payrate;

private double hours;

private double grosspay;


    public void setName(String nam)

    {

        String Name = nam;


    }

    public void setID(int id)

    {

        int ID = id;


    }

    public void setPayRate(String pay)

    {

        double PayRate = Double.parseDouble(pay);


    }

    public void setHours(String hou)

    {

        double Hours = Double.parseDouble(hou);


    }

    public String getName()
    {

        return name;

    }

    public int getID()
    {

        return ID;

    }

    public double getPayRate()
    {

        return payrate;

    }

    public double getHours()
    {

        return hours;

    }


    public double Gross()
    {

        double Gross = hours * payrate;

        grosspay = Gross;

        return grosspay;

    }






}
4

1 回答 1

1

怎么样而不是重新声明你的变量......

public void setName(String nam)
{
    String Name = nam;
}

public void setID(int id)
{
    int ID = id;
}

public void setPayRate(String pay)
{
    double PayRate = Double.parseDouble(pay);
}

public void setHours(String hou)
{
    double Hours = Double.parseDouble(hou);
}

您改用类实例字段...

public void setName(String nam)
{
    name = nam;
}

public void setID(int id)
{
    ID = id;
}

public void setPayRate(String pay)
{
    payRate = Double.parseDouble(pay);
}

public void setHours(String hou)
{
    hours = Double.parseDouble(hou);
}
于 2014-12-03T02:49:15.887 回答