这个 java 程序可能有什么错误?

What might be the error in this java program?

public class One {
   static String s1="hello";
   static String s2="world";

   String display(s1,s2){ return s1+s2; }

   public static void main(String[] args) {

    String s3=display(s1,s2);
    System.out.println(s3);
   }
}

我正在尝试通过将两个字符串传递给用户定义的方法来连接它们,但它不起作用!!! 我知道不需要任何这样的方法,但我请求任何人帮助我,我想看看如何将两个字符串传递给方法,然后如何使用 + operator[=12=return 连接字符串]

创建一个对象并调用显示方法

One one = new One();
String s3=one.display(s1,s2);

希望对您有所帮助。

同时声明参数并初始化它们

使方法显示静态。

public class One {
   static String s1="hello";
   static String s2="world";

   static String display(s1,s2){ return s1+s2; }

   public static void main(String[] args) {

    String s3=display(s1,s2);
    System.out.println(s3);
   }
}

您的 display 方法不是 static。通过更改

使其成为 static
String display(s1,s2)

static String display(s1,s2)

或使用

创建class的实例
One o=new One();

并使用

调用方法
o.display(s1,s2);

您还必须在参数前指定类型。所以改变

String display(s1,s2)

String display(String s1,String s2)

此外,由于 s1s2 是全局变量,它们不需要作为参数传递给 display 方法。