作者tryPTT (NULL)
看板Eclipse
標題[問題] 不好意思..請給我一點協助
時間Sun May 3 11:51:34 2009
感謝剛剛的回答,
在function外我加入 throws IOException 便除去該錯誤了。
不過我想問個問題,
import java.io.* ;
class Searcher {
int [] data ;
int item ;
int search( int data[], int item ) {
this.data = data ;
this.item = item ;
return binarySearch( 0, data.length - 1 ) ;
} // search()
int binarySearch( int bottom, int top ) { <-------------錯誤
if ( bottom > top ) { // cannot to find it
return -1 ;
} // if()
int middle = ( bottom + top ) / 2 ; // get the middle index
if ( item == data[middle] ) { // find the answer
return middle ;
} // if(()
else if ( item > data[middle] ) { // the answer at the right part
return binarySearch( middle + 1, top ) ;
} // else if()
else if ( item < data[middle] ) { // the answer at the left part
return binarySearch( bottom, middle - 1 ) ;
} // else if()
} // binarySearchData()
} // Searcher()
public class BinarySearch {
public static void main(String[] args) throws IOException {
Searcher toFind = new Searcher() ;
System.out.println( "Please import the data that you want to find : " ) ;
BufferedReader input = new BufferedReader( new InputStreamReader(
System.in ) ) ;
int target = java.lang.Integer.parseInt( input.readLine() ) ;
int dataArrayAnswerIndex = toFind.search( new int[] { 1, 3, 5, 7, 9 },
target ) ;
if ( -1 == dataArrayAnswerIndex ) {
System.out.println( "Cannot find " + target + " this data" ) ;
} // if()
else {
System.out.println( "This data : " + target + "'s index is " +
dataArrayAnswerIndex ) ;
} // else()
} // main()
} // binarySearch()
這是書上的範例程式
但是編譯時說那邊有錯誤(有標記了)
不過實在找不出還會有什麼問題
錯誤訊息為:This method must return a result of type
好怪......不好意思問題有點容易
我是新手..請多多包涵XD
感謝!!!
--
才過二十歲,牠就顯得有氣無力....。 (徵求線上(or Ptt)棋院對局)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.57.78.11
→ qrtt1:這問題去 java 版問吧 05/03 11:58
推 lpoijk:就是.. 你沒給return阿... 05/03 12:41
→ lpoijk:這邊是Eclipse版 請去JAVA吧 最後一個 } 前加個 return -1; 05/03 12:42
推 master0101:if elseif elsif 如果是其他的else會沒return 05/06 20:39
→ master0101:不然就把第二個elseif 改成else就好了 05/06 20:40