![](/img/trans.png)
[英]The method println(double) in the type PrintStream is not applicable for the arguments (String, double)
[英]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()
是靜態的,它是如何運行的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.