gpt4 book ai didi

java - 核心 Java 中的编译时错误

转载 作者:行者123 更新时间:2023-12-01 22:33:08 26 4
gpt4 key购买 nike

//为什么当我调用 hello 方法时会显示编译时错误?

public class Test {

public static void main(String [] args) {

System.out.println(hello());
}

public static void hello() {

System.out.println("from hello");
}
}

最佳答案

因为 hello() 不会返回 StringObject 或各种 println 接受的任何其他类型 PrintStream 中定义的方法.

事实上,您可以将任何类型的参数传递给 println 并获得某种形式的输出,但由于方法的返回类型被声明为 void,您实际上并没有传递参数。

您可以通过将 hello() 方法更改为类似以下内容来消除错误:

public static String hello() {
return "Hello world";
}

或者将方法调用更改为:

System.out.println();

关于java - 核心 Java 中的编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27310542/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com