所以,当我运行它时,它给了我毛钱,但是当我这样做时它只有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;
}
}
答案 0 :(得分: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);
}