【问题标题】:The method println(boolean) in the type PrintStream is not applicable for the arguments (void)PrintStream 类型中的方法 println(boolean) 不适用于参数 (void)
【发布时间】:2017-04-16 10:34:26
【问题描述】:

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

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)

【问题讨论】:

    标签: java


    【解决方案1】:

    您的 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();
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      Println() 函数期望某些东西,而您的方法不返回任何东西。这就是你得到错误的原因。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

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

          类型 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());
          
              }
          
          }
          

          它会起作用,因为它返回的字符串类型值不是 void。

          【讨论】:

            【解决方案5】:

            您想打印字符串(“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());
            
                }
            
            }

            【讨论】:

              【解决方案6】:
              package Abc;
              
              public class Class3 {
                  public static void another(){
                      System.out.println("Hello World!");
                  }
                  public static void main(String[] args) {
                      another();
                  }
              }
              

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

              【讨论】:

                【解决方案7】:

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

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

                您已完成,它将重新构建项目,您可以开始了。谢谢。

                【讨论】:

                • 这是不正确的,System.out.print在所有版本中都是一样的,你也试过你配置后的代码吗?我不这么认为,除此之外,您对上面所有正确答案都投了反对票❎这是错误的操作。
                • 首先只是将你的jdk从1.8降级到1.7是错误的,因为人们现在使用的是未来版本而不是旧版本,其次,我不说但是文档说读Java7, Java8, .. Java10 的文档,最后我不是来评判你的,我不是最好的你,但很抱歉你在这部分错了:)
                • 在对所有其他正确答案投了反对票之后,您向 OP 提供了错误答案。我可能听起来很咄咄逼人,但你应该被举报和禁止。
                猜你喜欢
                • 1970-01-01
                • 2012-06-24
                • 2014-06-20
                • 2011-12-08
                • 2013-03-20
                • 1970-01-01
                • 2016-05-28
                相关资源
                最近更新 更多