构造函数不能应用于给定类型,不需要参数;发现整数?

Constructor cannot be applied to given types, required no argument ; found int?

我正在制作一个 class 将罗马数字转换为阿拉伯数字,我的代码符合要求,但是当我尝试让客户端 class 对其进行测试时,我收到了错误 "constructor RomanNumerals in class RomanNumerals cannot be applied to given types; required: no arguments; found: int; reason: actual and formal argument lists differ in length".

这是我调用构造函数的代码

public class RomanConverter {

   public static void main(String[] args) {

      TextIO.putln("Enter a Roman numeral and this will change it to an");
      TextIO.putln("arabic integer.  Enter an integer.")

      while (true) {

         TextIO.putln();
         TextIO.put("? ");


         while (TextIO.peek() == ' ' || TextIO.peek() == '\t')
            TextIO.getAnyChar();
         if ( TextIO.peek() == '\n' )
            break;


         if ( Character.isDigit(TextIO.peek()) ) {
            int arab = TextIO.getlnInt();
            try {
                RomanNumerals N = new RomanNumerals(arab);
                TextIO.putln(N.toInt() + " = " + N.toString());
            }

这是构造函数

 public void RomanNumerals(int arabic){
        num = arabic;
    }

您正在尝试使用尚未定义的 重载 构造函数实例化 RomanNumerals 对象。

这个

public void RomanNumerals(int arabic){
    num = arabic;
}

实际上是一个方法,而不是构造函数。您需要这样定义构造函数:

public RomanNumerals(int arabic){
    // Initialization
}

这不是构造函数:

public void RomanNumerals(int arabic)

这是一个名为 RomanNumerals 的方法,它接受一个 int。 no-argument 构造函数是 Java.

为您创建的默认构造函数

您只需删除 void 即可解决问题。

此消息意味着预期的构造函数是没有任何参数的 RomanNumerals(),并且实际构造函数 RomanNumerals(int) 未定义。换句话说,形式参数列表是空的,但实际参数列表是一个int。

constructor RomanNumerals in class RomanNumerals cannot be applied to given types; required: no arguments; found: int; reason: actual and formal argument lists differ in length

构造函数应该如下所示。

public class MyClass {
  public MyClass(int x, double y) {
    ...
  }
}

请注意,没有 return 个参数。

由于 return 参数,您识别为构造函数的实际上是一个方法。