-2

ほぼ完了しましたが、コンパイルすると、「クラス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());
}
4

2 に答える 2

1
userTax = test.getUserTax();

doubleこの呼び出しにパラメーターとして値を渡す必要があります。タイプパラメータgetUserTaxとして定義されたメソッドが必要です。double

public static double getUserTax(double income)

例:

userTax = test.getUserTax(10.0);//ここで10.0はほんの一例です。

于 2012-11-30T22:57:53.717 に答える
0

このメソッドシグネチャを変更するだけです:-

public static double getUserTax(double income)

に: -

public static double getUserTax()

クラスにインスタンス属性としてincomeすでにあるため、パラメータを渡す必要はありません。そして、インスタンスでこのメソッドを呼び出すと:-incomeIncomeTaxIncomeTax

test.getUserTax();

incomeメソッドで使用されるのは、インスタンス属性を参照するに他なりませんthis.income

于 2012-11-30T22:59:14.907 に答える