I have this recursive method that counts the amount of negative numbers that lie in the array called "NumArray"
public static int countNegative(double[] NumArray, int startIndex, int endIndex)
{
if (startIndex == endIndex)
{
if (NumArray[startIndex] < 0)
{
return 1;
}
else
return 0;
}
else if (NumArray[endIndex] < 0)
{
return 1 + countNegative(NumArray, startIndex, endIndex - 1);
}
else
return countNegative(NumArray, startIndex, endIndex - 1);
In my main code I use this print state to print it:
int negCount = countNegative(NumArray, 0, NumArray.length -1); //setting negCount equal to the method
System.out.println ("The total count of negative numbers is " + negCount + ('\n'));
When I start the program and I put in a negative numbers it won't print after when a 0 is entered //When 0 is entered it'll stop taking in inputs from the user and display the amount I have.
Am I doing something wrong? Is my recursive method not reading in negative values?