簡體   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