【问题标题】:Not sure how to fix "argument lists differ in length" [duplicate]不确定如何解决“参数列表长度不同” [重复]
【发布时间】:2017-10-13 15:04:53
【问题描述】:

我需要创建一个应用程序,通过接受用户的年收入和婚姻状况(此处分别存储为“字符串婚姻”和“双重收入”来计算纳税义务。然后我需要从我的主类传递这些变量,“ ShowTax”到一个单独的类“Tax”,它计算应纳税额并将该值返回给主类以输出给用户。我有 95% 的把握,我已经正确设置了所有内容,但我不断收到此错误:

Tax 类中的构造函数税不能应用于给定类型:

必填:无参数

找到:字符串,双精度

原因:实际参数列表和形式参数列表的长度不同

我不确定我做错了什么......

这是我的 ShowTax 课程中的相关代码:

import java.util.Scanner;
public class ShowTax {


public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    

    System.out.print("Please enter Annual Income: $");
    double income = sc.nextDouble();
    
    while (income < 0) {
        System.out.print("ERROR: Income cannot be negative: ");
        income = sc.nextDouble();
   
    }
    
    System.out.print("Please Enter Marital Status (S/M): ");
    String marital = sc.next();
    
    while (!("S".equals(marital) || "s".equals(marital) || "M".equals(marital) || "m".equals(marital))) {
        System.out.print("ERROR: Try Again: ");
        marital = sc.next();
        if ("S".equals(marital) || "s".equals(marital) || "M".equals(marital) || "m".equals(marital)){
            break;
                    
        }
    }
    
    Tax myTax = new Tax(marital, income);
    

    }
}

这是我的 Tax 课程中的代码:

public class Tax{

    public double Tax(String marital, double income)
    {
        double taxRate;
        double liability=0;
        if (("s".equals(marital)||"S".equals(marital))){
            if (income >= 49002){
                taxRate=75;
                liability = (income - (income*(taxRate/100)));
            }
            else if (income > 15000){
                taxRate=50;
                liability = income - (income*(taxRate/100));
            }
            else {
                taxRate=25;
                liability = income - (income*(taxRate/100));
            }
        }
        else if ("m".equals(marital)||"M".equals(marital)){
            if (income >= 49002){
                taxRate=74;
                liability = income - (income*(taxRate/100));
            }
            else if (income > 15000){
                taxRate=49;
                liability = income - (income*(taxRate/100));
            }
            else {
                taxRate=24;
                liability = income - (income*(taxRate/100));
            }
        }
        return liability;
    }
}

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • public double Tax(...) 不是构造函数。这是一个名为Tax 的方法,它返回一个double
  • public double Tax(...) 不是构造函数。
  • 构造函数没有返回类型

标签: java compiler-errors


【解决方案1】:

正如所有其他答案所指出的,您创建了一个名为 Tax 的方法,而不是构造函数。

发生错误actual and formal argument lists differ in length 的实际原因是,当您不为类提供构造函数时,Java 会为您创建一个不带参数的构造函数。此默认构造函数与您尝试调用的构造函数的签名不匹配。

【讨论】:

    【解决方案2】:

    看起来在您的 main() 方法中,您有以下代码行:

    Tax myTax = new Tax(marital, income); 
    

    如果您的Tax 类有一个有两个参数的构造函数,这将起作用。您的 Tax 类中确实有一个方法,该方法带有两个参数,但这不是您的构造函数。

    public double Tax(String marital, double income)
    

    这有点令人困惑,因为方法和类都具有相同的名称。您可以尝试使用以下代码,看看是否有帮助:

    public class Tax{
    
    private String marital;
    private double income;
    
    Tax(String marital, double income)
    {
        this.marital = marital;
        this.income = income;
    }
    
    public double CaculateTax()
    {
        double taxRate;
        double liability=0;
        if (("s".equals(marital)||"S".equals(marital))){
            if (income >= 49002){
                taxRate=75;
                liability = (income - (income*(taxRate/100)));
            }
            else if (income > 15000){
                taxRate=50;
                liability = income - (income*(taxRate/100));
            }
            else {
                taxRate=25;
                liability = income - (income*(taxRate/100));
            }
        }
        else if ("m".equals(marital)||"M".equals(marital)){
            if (income >= 49002){
                taxRate=74;
                liability = income - (income*(taxRate/100));
            }
            else if (income > 15000){
                taxRate=49;
                liability = income - (income*(taxRate/100));
            }
            else {
                taxRate=24;
                liability = income - (income*(taxRate/100));
            }
        }
        return liability;
    }
    }
    

    【讨论】:

      【解决方案3】:

      您正在尝试创建 Tax 类的对象

      Tax myTax = new Tax(marital, income);
      

      这期望 Tax 类有一个构造函数

      public Tax(String marital, double income){...}
      

      事实并非如此!

      【讨论】:

        【解决方案4】:

        在 Java 中,您可以为 Tax 类定义一个参数化构造函数,如下所示:

        public Tax(String marital, double income) {
            // …
        }
        

        这允许您创建Tax 类的新实例。但是,您似乎正在尝试计算应纳税额(double 值),而不是创建新的对象实例?

        在这种情况下,您可能需要一个静态方法,例如:

        public class Tax {
            public static double liability(String marital, double income) {
                // …
            }
        }
        

        你可以通过说double myTax = Tax.liability(marital, income);来调用它

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-08
          • 1970-01-01
          • 2017-06-22
          • 2015-10-26
          • 2017-10-18
          • 1970-01-01
          • 1970-01-01
          • 2022-10-07
          相关资源
          最近更新 更多