在调用方法时协助零售税务实验室(java)

时间:2014-12-17 04:51:35

标签: java methods

以下是本实验的输出。我想知道我是否能得到一些帮助。我是Java的初学者,我需要对正确调用方法做一些澄清。任何指针都会有所帮助。

Welcome to the tax collector program!
Enter the total sales for the month
600
The county tax is: 0.0
The state tax is: 0.0
The total tax is 0.0
The county tax is 0.0
The state tax is 0.0
The total tax is 0.0

以下是我的代码。我对如何修复它感到困惑。

import java.util.*;
public class retailTax {
public static void main(String[] args){

    Scanner input = new Scanner(System.in);
    double monthlySales = 0;
    double countyTax = 0;
    double stateTax = 0;
    double totalTax = 0;
    System.out.println("Welcome to the tax collector program!");

    inputData(monthlySales);
    calcCounty(monthlySales, countyTax);
    calcState(monthlySales, stateTax);
    calcTotal(stateTax, countyTax, totalTax);
    printData(countyTax, stateTax, totalTax);

}
public static void inputData(double monthlySales)
{
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the total sales for the month");
    monthlySales = input.nextDouble();
}
public static void calcCounty(double monthlySales, double countyTax)
{
    countyTax = monthlySales * .04;

}
public static void calcState(double monthlySales, double stateTax){
    stateTax = monthlySales * .02;

}
public static void calcTotal(double countyTax, double stateTax, double totalTax)
{
    totalTax = countyTax + stateTax;    

}

public static void printData(double countyTax, double stateTax, double totalTax)
{
    System.out.println("The county tax is " + countyTax);
    System.out.println("The state tax is " + stateTax);
    System.out.println("The total tax is " + totalTax);
}

}

2 个答案:

答案 0 :(得分:0)

写作时

calcCounty(monthlySales, countyTax);

它正在使用您刚刚创建的方法,因此使用calcCounty(0,0)调用此方法。这是因为在inputData()内你的行monthlySales = input.nextDouble();正在设置你传入的参数的输入,而不是main中的变量。您在main中声明的变量只能在那里访问。你需要让它们成为类变量

public class retailTax {
    Scanner input = new Scanner(System.in);
    double monthlySales = 0;
    double countyTax = 0;
    double stateTax = 0;
    double totalTax = 0;

    public static void main(String[] args){
        System.out.println("Welcome to the tax collector program!");
        //and so on

接下来,您不应该使方法的参数与这些变量具有相同的名称。它不仅会变得混乱,而且在很多情况下会导致问题。另一个选择是让你的方法返回值而不是在其中分配它们。例如,我会改变

public static void inputData(double monthlySales)

public static double inputData() {
    //Scanner input = new Scanner(System.in); //You don't need this, you already made it
    System.out.println("Enter the total sales for the month");
    return input.nextDouble();
}

并像

一样使用它
monthlySales = inputData();

旁注

习惯上以大写字母RetailTax开头命名您的类。

答案 1 :(得分:0)

正如@Takendarkk所提到的,在main方法中声明的变量是局部变量。以下是更新的程序,具有调用方法的正确方法。此外,它是一个很好的java编码实践,让类名以大写字母开头。

以下是更新后的计划:

import java.util.*;
public class RetailTax {
    public static void main(String[] args){
        RetailTax rt = new RetailTax();
        double monthlySales = 0;
        double countyTax = 0;
        double stateTax = 0;
        double totalTax = 0;
        System.out.println("Welcome to the tax collector program!");

        monthlySales = rt.inputData();
        countyTax = rt.calcCounty(monthlySales);
        stateTax = rt.calcState(monthlySales);
        totalTax = rt.calcTotal(countyTax, stateTax);
        rt.printData(countyTax, stateTax, totalTax);

    }
    public double inputData()
    {
        System.out.println("Enter the total sales for the month");
        Scanner input = new Scanner(System.in);
        Double monthlySales = input.nextDouble();
        return monthlySales;
    }
    public double calcCounty(double monthlySales)
    {
        Double countyTax = monthlySales * .04;
        return countyTax;

    }
    public double calcState(double monthlySales){
        Double stateTax = monthlySales * .02;
        return stateTax;

    }
    public double calcTotal(double countyTax, double stateTax)
    {
        Double totalTax = countyTax + stateTax;    
        return totalTax;

    }

    public void printData(double countyTax, double stateTax, double totalTax)
    {
        System.out.println("The county tax is " + countyTax);
        System.out.println("The state tax is " + stateTax);
        System.out.println("The total tax is " + totalTax);
    }
}