数値を取る配列があります。私の方法の 1 つは、配列内の正の数の数を数えることです。したがって、プログラムを終了するために 2 3 4 5 6 と 0 を入力すると、Positive Numbers: 5 が出力されるはずですが、代わりに Positive Numbers : 4 が出力されます。最後の数字が抜けています。ただし、 2 3 4 5 -1 4 0 {0 終了} を実行すると、正の数の正しい数 (この場合は 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);
}
}