2017-09-30 72 views
-4

我想在不使用分號的情況下打印「hello」。使用for循環打印時你好我知道println和printf都可以用於打印你好。但是,如果只有printf用於打印hello.When在if語句中使用println時,它顯示「void type not permitted here」。這個錯誤的含義是什麼以及爲什麼println不能在if語句中使用?爲什麼在打印hello時使用「printf」而不是「println」if語句

public class withoutsemicolon 
{ 
public static void main(String [] args) 
{ 
for(int i=0;i<=4;System.out.println("hello"+i+"\n"),i++) 
{} 
if(System.out.println("hello"+"\n")!=null) 
{} 
} 
} 
+0

因此,先生普爾斯告訴我如何打印你好,而不使用分號 – user8340003

+0

爲什麼?爲什麼?請告訴我爲什麼你要這樣做? – Oleg

+0

在採訪中詢問了問題 – user8340003

回答

1

原因是printf回報PrintWriter,你可以比較nullprintln返回void即不能與null比較的任何內容。在程序中不使用分號打印的其他選項有whiletry。所有上面提到的選項:

if(System.out.printf("hello\n") == null){} 
while(System.out.printf("hello\n") == null){} 
try(Closeable c = System.out.printf("hello\n")){} 

一個額外的選擇,我最喜歡的就是告訴你拒絕回答愚蠢的問題主考官,但這是一個奢侈並不是每個人都具備。

+0

謝謝先生,現在我可以肯定地在採訪中回答這個問題 – user8340003

相關問題