【发布时间】:2015-10-15 15:43:37
【问题描述】:
由于某种原因,没有计算方法 grossPay() 和 fedTax()。
- 如何返回
grossPay()结果grosspay? - 如何返回
fedTax()结果taxtotal? - 我将在哪里计算
totalpay = grosspay - taxtotal? - 由于我无法返回
taxtotal,我无法判断它是否正常工作,但我是否正确调用grosspay和dependents来计算taxtotal?
谢谢!
import java.util.Scanner;
public class WorkPay {
public static void main(String args[]) {
WorkPay wagecalc = new WorkPay(); // 1. Instantiate the object WorkPay
WorkPay input = new WorkPay(); // 2. Reference the method to prompt for inputs
input.prompt4data();
input.display(); // 3. Reference the method to display the results
}
public void prompt4data() {
Scanner console = new Scanner(System.in);
System.out.println("How many hours have you worked?");
hours = console.nextDouble();
System.out.println("What is your wage rate?");
wage_rate = console.nextDouble();
System.out.println("How many dependents do you have?");
dependents = console.nextInt();
}
// private instance variables
private double hours;
private double wage_rate;
private int dependents;
private double grosspay;
private double totalpay;
private double tax;
private double dependenttax;
private double taxtotal;
public double grossPay() {
double total1 = 0;
double total2 = 0;
double total3 = 0;
if (hours <= 40) {
total1 = wage_rate * hours;
grosspay = total1;
}
else if (hours > 40 && hours <= 60) {
total2 = total1 + (wage_rate * 1.5) * (hours - 40);
grosspay = total2;
}
else {
total3 = total2 + (wage_rate * 2) * (hours - 60);
grosspay = total3;
}
return grosspay;
}
public void fedTax() {
tax = (0.10 * grosspay);
dependenttax = (25 * dependents);
taxtotal = tax + dependenttax;
if (tax < 0)
System.out.println("Taxt withheld can't be less than 0.");
}
public void display() {
System.out.println("The hours worked is: " + hours);
System.out.println("The hoursly rate is: " + wage_rate);
System.out.println("The number of dependents is: " + dependents);
System.out.println("The gross income is: " + grosspay);
System.out.println("The federal tax withheld is: " + taxtotal);
System.out.println("The take home pay is: " + totalpay);
}
}
【问题讨论】:
-
wagecalc在哪里使用?没有在哪里。为什么会在那里?使用 IDE 并注意警告。 -
好吧,你永远不会调用这些方法。你应该打电话给他们,这样他们就可以设置值。此外,您应该选择不同的名称,因为现在方法名称与变量名称几乎相同。 “calculateCrossPay”或类似的怎么样?
标签: java