我有一个带数字的数组。我的一种方法是计算数组中正数的数量。因此,如果他们输入 2 3 4 5 6 和 0 来终止程序,它应该打印出正数:5,但它会打印出正数:4。它错过了最后一个数字。但是,如果我执行 2 3 4 5 -1 4 0 {0 terminates},它会打印出正确数量的正数,在本例中为 5。我已经进行了一些调试,但似乎无法弄清楚。有什么帮助吗?
public static void main (String args[]) throws IOException
{
int i = 0;
int [] nums;
nums = new int [100];
InputStreamReader inRead = new InputStreamReader(System.in); //
BufferedReader buffRead = new BufferedReader(inRead);
String line = buffRead.readLine();
while (line.equals("0") == false && i<100)
{
i++;
line = buffRead.readLine();
nums[i]=(int) Double.parseDouble(line);
}
System.out.print ("The minimum number is " + findMin (nums, 0, nums.length - 1) + ('\n'));
System.out.println("Sum of the numbers at odd indices: " + computeSumAtOdd(nums, 0 , nums.length -1 ) + ('\n') );
System.out.println("The total count of positive numbers is " + countPositive(nums,0,nums.length -1));
}
public static int countPositive(int[] numbers, int startIndex, int endIndex)
{
if (startIndex == endIndex)
{
if (numbers[startIndex] > 0)
{
return 1;
}
else
return 0;
} else
{
if (numbers[startIndex] > 0)
{
return 1 + countPositive(numbers, startIndex +1, endIndex);
}
else
return countPositive(numbers, startIndex +1, endIndex);
}
}