Java Simple Tax Calculator,调用方法

时间:2016-02-19 23:07:47

标签: java methods calculator

我创建了一个简单的税收计算器,计算联邦税和省税的收入小于或等于41,536美元,这个工作正常。 现在要创建一个计算和打印税的方法,这对我不起作用

这是代码..

package lab4;

import java.util.Scanner;
/**
 *
 * @author demo
 */
public class Lab4 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
    // Q4. Tax Calculation
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter your income: ");
    String userIncome=sc.nextLine();

    calculateAndPrintTax();
}       

static double calculateAndPrintTax(double userIncome)
{

    double uIncome= Double.parseDouble(userIncome);
    double federalExemption= 11327.0;
    double provincialExemption= 9863.0;
    double federalTax =  (uIncome- federalExemption) * 0.15;
    double provincialTax= (uIncome - provincialExemption) * 0.0505;
    double totalTax= federalTax + provincialTax;

    System.out.println("Your payable Federal tax is: " + federalTax);
    System.out.println("Your payable Provincial tax is: "+ provincialTax);
    System.out.println("Total payable tax is: "+ totalTax);
}

}

2 个答案:

答案 0 :(得分:2)

我改变了一些东西,并且能够成功编译并运行程序。

public static void main(String[] args) 
{
// Q4. Tax Calculation
Scanner sc= new Scanner(System.in);
System.out.print("Enter your income: ");
double userIncome=sc.nextDouble();

calculateAndPrintTax(userIncome);
}       

static void calculateAndPrintTax(double userIncome)
{
double federalExemption= 11327.0;
double provincialExemption= 9863.0;
double federalTax =  (userIncome- federalExemption) * 0.15;
double provincialTax= (userIncome - provincialExemption) * 0.0505;
double totalTax= federalTax + provincialTax;

System.out.println("Your payable Federal tax is: " + federalTax);
System.out.println("Your payable Provincial tax is: "+ provincialTax);
System.out.println("Total payable tax is: "+ totalTax);
}
}

当你调用calculateAndPrintTax方法时,你也必须输入参数,因为你的方法要求它们。所以代替calculateAndPrintTax();你应该有calculateAndPrintTax(userIncome);因为这是该方法用于完成所有计算的内容。我还将userIncome更改为double以简化过程。

你还必须使calculateAndPrintTax为void,并删除double,因为它不会返回任何值,而是将它们打印出来。

希望我能提供帮助。

答案 1 :(得分:1)

public static void main(String[] args) {


    Scanner sc= new Scanner(System.in);
    System.out.print("Enter your income: ");
    String userIncome=sc.nextLine();       

    calculateAndPrintTax(userIncome);
}       

static void calculateAndPrintTax(String userIncome)
{

    double uIncome= Double.parseDouble(userIncome);
    double federalExemption= 11327.0;
    double provincialExemption= 9863.0;
    double federalTax =  (uIncome- federalExemption) * 0.15;
    double provincialTax= (uIncome - provincialExemption) * 0.0505;
    double totalTax= federalTax + provincialTax;

    System.out.println("Your payable Federal tax is: " + federalTax);
    System.out.println("Your payable Provincial tax is: "+ provincialTax);
    System.out.println("Total payable tax is: "+ totalTax);
}