【问题标题】:How do I make variables work with multiple methods?如何使变量与多种方法一起使用?
【发布时间】:2015-10-15 15:43:37
【问题描述】:

由于某种原因,没有计算方法 grossPay()fedTax()

  1. 如何返回grossPay() 结果grosspay
  2. 如何返回fedTax() 结果taxtotal
  3. 我将在哪里计算 totalpay = grosspay - taxtotal
  4. 由于我无法返回taxtotal,我无法判断它是否正常工作,但我是否正确调用grosspaydependents 来计算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


【解决方案1】:

要么调用它们,要么使用getter(更好),getter,又名访问器,是一种命名约定,其中方法以get为前缀(另见Java - Using Accessor and Mutator methods):

public double getGrossPay() {
    double grosspay = 0; //local now (remove field)
    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 display() {
    ...
    System.out.println("The gross income is: " + getGrossPay());
    ...
}

这样总工资总是正确的,没有过时的字​​段。

getGrossPay 还存在一些其他问题,各种小计并未全部计算出来,但我会让你解决这个问题,或者你看看这对你是否有意义:

public double getGrossPay() {
    double grosspay = wage_rate * hours;
    if (hours > 40) grosspay += wage_rate * (hours - 40) * 0.5;
    if (hours > 60) grosspay += wage_rate * (hours - 60) * 0.5;
    return grosspay;
}

【讨论】:

    【解决方案2】:

    让我们从您的领域开始。您只需要其中几个。

    // private instance variables
    private double hours;
    private double wage_rate;
    private int dependents;
    

    这三个字段(wage_rate 应该是 wageRate 以符合惯例)是您唯一需要的字段。应计算所有其他值。

    接下来,由于您计算的值相互依赖(doubleTime 包括您的基本费率和时间半费率),您应该预先计算这些值。这不应该有任何风险,因为例如,如果您只有 51 小时的工作,您就不会在乎 doubleTime 是否不准确。

    public double grossPay() {
        double baseRate = wage_rate * hours;
        double timeAndAHalf = baseRate + (wage_rate * 1.5) * (hours - 40);
        double doubleTime = timeAndAHalf + (wage_rate * 2) * (hours - 60);
        if (hours <= 40) {
            return baseRate;
        } else if (hours > 40 && hours <= 60) {
            return timeAndAHalf;
        } else {
           return doubleTime;
        }
        return grosspay;
    }
    

    第三,我们来仔细看看fedTax这个方法。好消息是您不会在其他任何地方使用dependenttax真的应该是dependentTax)或tax,所以您需要做的就是明确返回 em> 您计算的值。不要忘记实际调用 grossPay 方法,因为您需要将其作为计算的一部分。

    public double fedTax() {
        tax = (0.10 * grossPay());
        dependenttax = (25 * dependents);
        return tax + dependenttax;
    }
    

    最后,剩下的就是打印您必须在输入字段的最后部分计算的值...

    // Method call to...something that does things with gross pay
    System.out.println("The gross income is: " + _____);
    // Method call to...something that does things with tax...
    System.out.println("The federal tax withheld is: " + _____);
    // What's the net pay again?
    System.out.println("The take home pay is: " + ____);
    

    ...但我将那个留给读者作为一个明确的练习。

    【讨论】:

      【解决方案3】:

      由于您使用实例变量进行所有计算,因此您不必从方法中返回。您可以继续更新变量并在最后打印它们。

      在这里,我对您的程序进行了一些更改。

      import java.util.Scanner;
      
          public class WorkPay {
      
              // 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 static void main(String args[]) {
                  WorkPay input = new WorkPay(); // 2. Reference the method to prompt for
                                                 // inputs
                  input.prompt4data();
                  input.grossPay();
                  input.fedTax();
                  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();
              }
      
              public void 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;
                  }
              }
      
              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);
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2012-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 2014-03-28
        • 2020-07-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多