繁体   English   中英

PrintStream 类型中的方法 println(boolean) 不适用于参数 (void)

[英]The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

当我尝试执行以下代码时出现错误:

package Abc;

public class Class3 {

    public void another() {
        System.out.println("Hello World");
    }

    public static void main(String[] args) {
        Class3 obj1 = new Class3();
        System.out.println(obj1.another());
    }

}

错误是:

The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

您的 another() 函数返回类型是“void”,它本质上表示它被定义为不返回任何内容。

package Abc;

public class Class3 {
    public void another() {
       System.out.println("Hello World");
    }

   public static void main(String[] args) {
    Class3 obj1 = new Class3();
    obj1.another();
    }

}

Println() 函数期待一些东西,而你的方法不返回任何东西。 这就是为什么你会出错。

您的另一种方法具有返回类型“void”,因此基本上它不返回任何内容。 所以你不能打印任何东西。如果你想让你的代码工作,你只需调用 obj1.another()。 除了 System.out.println() 方法。

我们可以调用System.out.println(boolean) 中的任何函数,它返回任何 Object、String、int、boolean、char、char[]、double、float、long 值。

PrintStream 类型中的 println(boolean) 方法不适用于任何具有 void 返回类型的函数。

package Abc;

public class Class3 {
 public String another(){
     return "Hello World";



 }
    public static void main(String[] args) {
        Class3 obj1 = new Class3();
        System.out.println(obj1.another());

    }

}

它会起作用,因为它返回 String 类型的值而不是 void。

你想打印字符串(“Hello World”)? 您可以使用IDE工具来帮助您轻松解决问题; 你不能打印两次,你需要退货。 像这样改变

 package Abc; public class Class3 { public String another(){ return "Hello World"; } public static void main(String[] args) { Class3 obj1 = new Class3(); System.out.println(obj1.another()); } }

package Abc;

public class Class3 {
    public static void another(){
        System.out.println("Hello World!");
    }
    public static void main(String[] args) {
        another();
    }
}

这就是你所要做的,我什至不知道如果没有another()是静态的,它是如何运行的。

它只是 jdk 1.8 的一个特性(不是大问题)为了从您的项目中消除这个错误,只需将您的 jdk 从 1.8 降级到 1.7,它就会开始正常运行。

步骤: 1. 右键单击​​项目/存储库 2. 单击属性 3. 单击 Java 编译器 4. 从下拉列表中选择 jdk 1.7 5. 单击应用并关闭按钮

你完成了,它会重建项目,你很高兴。 谢谢。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM