/*3. Find the elements in an ordered array using the dichotomy method. The returned index is found, and no output-1 is present. Using recursion to implement */ import java.util.scanner; public class recursion{ public static void main (String[]args) { system.out.println (" Please enter the integer to be searched for "); scanner sc=new scanner (system.in); int num = Sc.nextint (); int []a={1,2,3,4,5,6,7,8,9}; int front = 0; int end = a.length-1; int mid = (front+end)/2; // Recursive implementation of dichotomy int index=findarray (A,front,mid,end,num); if (index!=-1) { System.out.println ("Find the number" +index); } else { system.out.println ( index); } } public static int findarray (int[]b,int front,int& NBsP;mid,int end,int num) {int index = -1; if (front==end) {if (b[mid]==num ) {Return mid;} Else{return index;} } else if (front<end) { if (b[mid]==num) {index = Mid;return index;} Else if (B[mid]>num) {end = mid-1;mid = (front+end)/2;return findarray (B,front, Mid,end,num);} Else if (b[mid]<num) {front=mid+1;mid= (front+end)/2; return findarray (b, Front,mid,end,num); } } //why the following else needs to be commented out, without commenting out the compilation will be an error saying the missing return value else { return index; } // return index; } }
Why the program error, said else statement after the missing return value
This article is from the "11890208" blog, please be sure to keep this source http://11900208.blog.51cto.com/11890208/1865846
Java recursive implementation of the dichotomy, I hope you point out