作者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
1F:→ qrtt1:这问题去 java 版问吧 05/03 11:58
2F:推 lpoijk:就是.. 你没给return阿... 05/03 12:41
3F:→ lpoijk:这边是Eclipse版 请去JAVA吧 最後一个 } 前加个 return -1; 05/03 12:42
4F:推 master0101:if elseif elsif 如果是其他的else会没return 05/06 20:39
5F:→ master0101:不然就把第二个elseif 改成else就好了 05/06 20:40