I have a method that checks if a given integer array is alternating as in positive and negative numbers. This method takes in an integer array as the parameter and I need to pass in the array as a set of numbers instead of the usual way where I assign the array that is being passed in, to a variable and then passing it in.
The code is below.
public class PositivesNegatives {
public static void main(String[] args) {
alternateSign([3, -2, 5, -5, 2, -8]);
alternateSign([-6, 1, -1, 4, -3]);
alternateSign([4, 4, -2, 3, -6, 10]);
}
private boolean alternateSign(int[] num) {
boolean t = false;
for(int i = 0; i < num.length; i++) {
if(num[0] < 0 && num[0]!= 0) {
if(i % 2 != 0) {
if(num[i]>0)
t = true;
}
}else if(num[0] > 0 && num[0] != 0) {
if(i % 2 != 0) {
if(num[0]<0)
t = true;
}
}
}
return t;
}
}
I tried searching it up but couldn't find an answer.