我正在运行一个基于数字输入的数组。它使用递归来查找数组上的最小值。如果用户输入 0,则程序结束。在我处理负数之前它工作正常,比如说我输入 0 2 3 5 3。返回正确,最小的数字将是 0。但是如果我输入 2 9 92 2 -1 0。程序不会在我输入后结束0 因此它不会将 -1 显示为最小数字。任何建议或帮助。
import java.io.*;
import java.text.*;
public class Assignment9
{
public static void main (String args[]) throws IOException
{
int i = 0;
double[] NumArray;
NumArray = new double[100];
// input stream reader reads in keyboard inputs, while buffered reader
// reads in the line as a string15.
InputStreamReader inRead = new InputStreamReader(System.in);
BufferedReader buffRead = new BufferedReader(inRead);
String line = buffRead.readLine();
// if the string is equal to 0 and is false AND i is less than22.
// 100, parse string into double.23.
try
{
while (line.equals("0") == false && i<100)
{
i++;
line = buffRead.readLine();
NumArray[i]=Double.parseDouble(line);
}
}
catch(IOException e)
{
System.out.println("Array index out of bound");
}
double min = findMin(NumArray, 0, NumArray.length - 1);
System.out.print ("The minimum number is " + min + ('\n'));
public static double findMin(double[] NumArray, int startIndex, int endIndex)
{
if (startIndex == endIndex)
{
return NumArray[startIndex];
}
else if(findMin(NumArray, startIndex, endIndex - 1) < NumArray[endIndex])
{
return findMin(NumArray, startIndex, endIndex -1 );
}
else
{
return NumArray[endIndex];
}
}