递归Java程序忽略if-else语句返回并返回3次
所以我在递归程序方面遇到了问题。程序所做的是将一个文本文件作为输入,其中包含一个未排序的数字列表。然后,它要求用户使用递归二进制搜索在数组中查找一个数字。如果找到该数字,则返回该数字的索引,如果没有,则返回-1。现在,对于任何输入,这个程序返回-1,即使它在数组中。我不明白的是,return-mid语句被调用,然后return-1被调用了几次。所以我很困惑我的程序是如何运行到一个return语句中的,但是忽略它然后返回-1。这是我使用的文本文件中的数字递归Java程序忽略if-else语句返回并返回3次,java,recursion,return,return-value,Java,Recursion,Return,Return Value,所以我在递归程序方面遇到了问题。程序所做的是将一个文本文件作为输入,其中包含一个未排序的数字列表。然后,它要求用户使用递归二进制搜索在数组中查找一个数字。如果找到该数字,则返回该数字的索引,如果没有,则返回-1。现在,对于任何输入,这个程序返回-1,即使它在数组中。我不明白的是,return-mid语句被调用,然后return-1被调用了几次。所以我很困惑我的程序是如何运行到一个return语句中的,但是忽略它然后返回-1。这是我使用的文本文件中的数字 31 70 5 71 140 187 16
31 70 5 71 140 187 162 98 153 8 109 103 145 157 27 23 136 54 19 168 114 25 139 129 94
这是程序本身
import java.io.*;
import java.util.*;
public class Lab2
{
public static void main (String[] args) throws Exception
{
if (args.length < 1)
{
System.out.println( "Fatal Error. Enter a filename on the command line!\n");
System.exit(0);
}
int[] arr = new int[30]; // don't do the resizing thing. Leave that to Project#1
int cnt=0;
Scanner file1 = new Scanner( new FileReader(args[0]) );
while ( file1.hasNextInt() )
arr[cnt++]= file1.nextInt();
file1.close();
// print the array as it came from the file
printArray( "original array: ", arr, cnt );
// sort using Arrays.sort (see utils API)
Arrays.sort( arr, 0, cnt ); // 2nd index non inclusive - i.e. cnt-1
// re-print it - should come out sorted
printArray( "sorted array: ", arr, cnt );
// now search the sorted array using YOUR bSearch
Scanner kbd = new Scanner( System.in );
do
{
System.out.print("Enter number to search for: ");
int key = kbd.nextInt();
if ( key <= 0) break; // ENTER ZERO OR NEGATIVE TO QUIT LOOP
int index=bSearch( arr, 0, cnt-1, key );
if ( index < 0 )
System.out.println( key + " not found at index: " + index);
else
System.out.println( key + " found at index: " + index);
}
while ( true ); // infinite loop. Must break to get out
} // END main
// ======================================================================
// M E T H O D S
// ======================================================================
// return the index where key was found
// else return -1 for not found
static int bSearch(int[] array, int low, int high, int key)
{
int mid;
if (low <= high)
{
mid = (low+high)/2;
if (array[mid] < key)
{
bSearch(array, mid+1, high, key);
}
else if (array[mid] > key)
{
bSearch(array, low, mid-1, key);
}
else {
System.out.println("this is true");
return mid;
}
}
System.out.println("why is this returning");
return -1;
}
// USE THIS METHOD AS GIVEN: DO NOT CHANGE
private static void printArray( String label, int[] array, int count )
{
System.out.print(label);
for( int i=0 ; i<count ;++i )
System.out.print(array[i] + " " );
System.out.println("\n");
}
import java.io.*;
导入java.util.*;
公共类Lab2
{
公共静态void main(字符串[]args)引发异常
{
如果(参数长度<1)
{
System.out.println(“致命错误。在命令行中输入文件名!\n”);
系统出口(0);
}
int[]arr=new int[30];//不要做调整大小的事情。把它留给Project#1
int-cnt=0;
Scanner file1=新扫描仪(新文件读取器(args[0]);
while(file1.hasNextInt())
arr[cnt++]=file1.nextInt();
file1.close();
//打印来自文件的数组
打印阵列(“原始阵列:”,arr,cnt);
//使用Arrays.sort进行排序(请参见utils API)
Arrays.sort(arr,0,cnt);//第二个索引不包含-即cnt-1
//重新打印-应该分类出来
printArray(“排序数组:”,arr,cnt);
//现在使用b搜索搜索已排序的数组
扫描仪kbd=新扫描仪(System.in);
做
{
System.out.print(“输入要搜索的编号:”);
int key=kbd.nextInt();
如果(键您错过了2个返回语句
if (array[mid] < key)
{
return bSearch(array, mid+1, high, key);
}
else if (array[mid] > key)
{
return bSearch(array, low, mid-1, key);
}
if(数组[mid]键)
{
返回b搜索(数组、低、中1、键);
}
您正在调用bsearch,但您没有返回答案。两行都说bsearch(…
应该说return bsearch(…
)哇,我知道我应该这样做,但我想我从来没有想过检查这部分。