方法参数不匹配:必需double,不传递参数

时间:2012-11-30 22:56:17

标签: java bluej

我差不多完成但是当我编译时我得到错误“类IncomeTax中的方法getUserTax不能应用于给定的类型;必需的double;找到:没有参数;原因:实际和形式参数列表的长度不同”我是多次查看我的代码,我无法找到错误所在的位置。它接近“userTax = test.getUserTax();

行代码的末尾
import java.util.Scanner;  //Needed for the Scanner Class
/**
 * Write a description of class IncomeTax here.
 * 
 * @author CD
 * 11/30/2012
 * The IncomeTax class determines how much tax you owe based on taxable income.
 */
public class IncomeTax
{
    private int income;

    /** 
     * The constructor accepts an argument for the income field.
     */
    public IncomeTax(int i)
    {
        income = i;
    }
    /**
    * The setIncome method accepts an argument for the income field.
    */
    public void SetIncome(int i)
    {
        income = i;
    }
    /**
     * The getIncome method returns the income field.
     */
    public double getIncome()
    {
        return income;
    }
    /**
     * The getUserTax returns the tax for the income.
     */
    public static double getUserTax(double income)
    {
        double userTax = 0.10;
        if (income > 250000.0) {
            userTax = 0.35;
        } else if(income > 130000.0) {
            userTax = 0.33;
        } else if(income > 60000.0) {
            userTax = 0.28;
        } else if(income > 30000.0) {
            userTax = 0.25;
        } else if(income > 10000.0) {
            userTax = 0.15;
        }
    return userTax;
}
/**
 * This program uses the IncomeTax class to determine the Income tax for the user's 
income.
 */
public static void main(String [] args)
{
    int userIncome; //To hold taxable income
    double userTax; //To hold tax

    //Create a Scanner object to read input.
    Scanner keyboard = new Scanner(System.in);

    //Get the Personal Income.
    System.out.print("Enter your taxable income and" + "I will tell you the income tax:");
    userIncome = keyboard.nextInt();

    //Create an IncomeTax object with the numeric score.
    IncomeTax test = new IncomeTax(userIncome);

    //Get the income tax.
    userTax = test.getUserTax();

    //Display the income tax.
    System.out.print("Your income tax is" + test.getUserTax());
}

2 个答案:

答案 0 :(得分:1)

userTax = test.getUserTax();

您需要将double值作为参数传递给此调用。您的getUserTax方法定义为double类型参数。

public static double getUserTax(double income)

示例:

userTax = test.getUserTax(10.0); //这里只是10.0。

答案 1 :(得分:0)

只需更改此方法签名: -

public static double getUserTax(double income)

来: -

public static double getUserTax()

您不需要传递任何income参数,因为您已在income类中将IncomeTax作为实例属性。当您在IncomeTax实例上调用此方法时: -

test.getUserTax();

方法中使用的income只是this.income,它引用了实例属性。