+ 2

Gotham city, whats wring with my code

public class Program { public static void main(String[] args) { int enemy=0; if(enemy<5){ System.out.print("I got this!"); } else if(enemy==5&&enemy<11){ System.out.print("Help me batman!"); } else if(enemy>10){ System.out.print("Goodluck out there!"); } } }

20th Sep 2020, 8:46 AM
jason Carzano
jason Carzano - avatar
4 Respuestas
+ 2
In the second else if, remove the first check and leave this: else if(enemy < 11)
20th Sep 2020, 8:47 AM
Bagon
Bagon - avatar
+ 1
The output string must be 100% the same as stipulated in specifications.
20th Sep 2020, 9:10 AM
Gordon
Gordon - avatar
0
Ok ty
20th Sep 2020, 9:10 AM
jason Carzano
jason Carzano - avatar
0
As your program works through the if/else block you know more about the value of enemy. If you get to the second test you know that enemy is 5 or more because the code did not print 'I got this!' and exit the if/else block. If you made the adjustment @ Bagon suggested and you arrive at the last [else if] you know that enemy is 11 or more, even without a test, because the program did not print 'Help me batman!' and exit the if/else block. Therefore, you could change the last [else if] to be simply an else without the test enemy>10 since enemy has to be greater than 10 to have arrived at that point in the if/else block. Remeber too what @ Gordon said the output must match the spec 100%
20th Sep 2020, 12:44 PM
Paul K Sadler
Paul K Sadler - avatar