2015-03-03 143 views
-1

我試圖編寫一個程序,如果文件索引不存在,它將打印出一條失敗消息。當我將這段代碼放入BlueJ時,它告訴我需要一個return語句。幫幫我?Java布爾返回語句

public boolean validIndex(int index) 
    { 
     if ((index >= 0) && (index <= files.size() - 1)) { 
      return true; 
     } 
     else { 
      System.out.println("File not found."); 
     } 
    } 
+1

所有的代碼路徑必須返回一些東西,你應該在(而不是?)打印後返回false。 – 2015-03-03 01:32:54

回答

0

在你的else語句中,它應該是「return false」。您可以返回您嘗試輸出的消息: System.out.println(「File not found。」);如果您正在做一個 - public String validIndex(int index) 您可以返回您嘗試輸出的消息: System.out.println

0

您還沒有爲else語句添加return l。要麼添加返回的if else語句或else語句

public boolean validIndex(int index) { 
if ((index >= 0) && (index <= files.size() - 1)) { 
return true; 
} else { 
System.out.println("File not found."); 
} 
return false; 
} 
0

您應該返回else中的內容。您將返回類型指定爲布爾值,因此該方法始終應返回boolean

public boolean validIndex(int index) 
{ 
     if ((index >= 0) && (index <= files.size() - 1)) { 
      return true; 
     } 
     else { 
      System.out.println("File not found."); 
      return false; 
     } 
}