public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(reader.readLine());
int b = Integer.parseInt(reader.readLine());
int c = Integer.parseInt(reader.readLine());
int countNeg = 0;
int countPos = 0;
for (int i = 0; i < 3; i++){
if(a < 0 || b < 0 || c < 0){
countNeg++;
}
else if(a > 0 || b > 0 || c > 0){
countPos++;
}
}
System.out.println("Number of negative numbers: " + countNeg);
System.out.println("Number of positive numbers: " + countPos);
}
}
When I type in 3 positive numbers it displays correctly. If I type in for example 2 positive numbers and 1 negative number it displays that number of negative numbers are 3. Why does it do that? :/Adam
Level 17
Either 3 of positive or negative numbers
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Adam
31 July 2020, 21:52
Oh okay I understand now! I thought I could maneuver this with the loop. But since int a, b and c always are either positive or negative or both, it will stop on the statement that can satisfy the condition. And then it won't move on to even check the other statement (else if). Which results in it either being 3 of either negative of positive. Thank you for the help!
0
Halnik111
31 July 2020, 22:23
yup. but u should use that loop anyways. i didnt test your code but it seems like all u have to do is remove that one "else if" and replace it with "if"
0
Halnik111
31 July 2020, 21:35useful
if {}
else if {}
"else if" statement means this: if one condition (starting from the top) gets satisfied then statement ends and wont continue. example:
+1
Misiu
31 July 2020, 20:22useful
If one (two, three) of numbers is (are) negative, output always will be Neg = 3. Three times the same first condition of IF.
Try IF separetly for any number.
+1