Trying to use conditionals instead of if statements for double comparison. Debugged step by step several times and can not figure out on what planet java thinks -9==0. When you highlight it, it says false but increases the "zero" double anyways.. The other ones seem to come come across just fine.
My input is:
6
-4 3 -9 0 4 1
The code:
public class HRWPlusMinus {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int length = Integer.valueOf(in.nextLine());
String[] input = in.nextLine().split(" ");
solveWOF(length, input);
}
//solve with for loop
public static void solve(int length, String[] input) {
double pos = 0;
double neg = 0;
double zero = 0;
for (int i = 0; i < length; i++){
int test = Integer.valueOf(input[i]);
if (test > 0) {pos++;}
else if (test < 0) {neg++;}
else {zero++;}
}
System.out.printf("%.6f \n%.6f \n%.6f", (pos/length), (neg/length), (zero/length));
}
//solve with conditionals
public static void solveWOF(Integer length, String[] input) {
double pos = 0, neg = 0, zero = 0;
for (int i=0; i<length; i++) {
Double test = Double.valueOf(input[i]);
zero = (test == 0)? zero++:
test > 0 ? pos++: neg++;
}
System.out.printf("%.6f \n%.6f \n%.6f", (pos/length), (neg/length), (zero/length));
}
}
Top method works, bottom one is the one I'm having issues with.