How does java evaluate the following code with the logical operator in java?
public class ApaBoleh{
public static void main(String[]args){
for(int i=1;i<=100;i++){
if(i%3==0){
System.out.print("Apa,");
}else if (i%5==0){
System.out.print("Boleh,");
}else if ((i%3==0)&&(i%5==0)){
System.out.print("ApaBoleh,");
}
System.out.print(i+",");
}
}
}
when I run this code, the following line doesn't run }else if ((i%3==0)&&(i%5==0)){
.
It's because you're using else
to exclude anything prior to that condition that resolves to true. Try checking the most exclusive case first:
if (( i%3 == 0 ) && ( i%5 == 0 )){
System.out.print("ApaBoleh,");
}else if ( i%3 == 0){
System.out.print("Apa,");
}else if ( i%5 == 0){
System.out.print("Boleh,");
}
System.out.print(i+",");
You have an else
where both preceding if
(s) are true
; only the first is entered. I would save the tests to local variables because repeating all of those modulus operations is not very clean. Like,
for (int i = 1; i <= 100; i++) {
boolean mod3 = i % 3 == 0, mod5 = i % 5 == 0;
if (mod3 && mod5) {
System.out.print("ApaBoleh,");
} else if (mod3) {
System.out.print("Apa,");
} else if (mod5) {
System.out.print("Boleh,");
}
System.out.print(i + ",");
}
See also The FizzBuzz challenge .
Before run
else if ((i%3==0)&&(i%5==0)){ System.out.print("ApaBoleh,");}
it runs
if(i%3==0){
System.out.print("Apa,");
}else if (i%5==0){
System.out.print("Boleh,");
so you have to put
else if ((i%3==0)&&(i%5==0)){ System.out.print("ApaBoleh,");}
before above two
Of course it'll not run, the moment one of your previous two if conditions evaluate to true
, you'll never get to that if block.
For example, if i = 3
then your first condition if(i%3==0)
will evaluate to true
and your print out Apa
, since the rest of the conditions are else if
the java code will stop processing and go to the next loop iteration. Similarly, if i = 5
then first condition will evaluate to false
and the second condition if(i%5==0)
will evaluate to true
and will not continue to the next else if
Hence if either of your previous conditions evaluate to true
you'll not get to your third condition.
Apart from this, if you had to remove the preceding two if
statemets and only evaluate that condition as follows:
public class ApaBoleh{
public static void main(String[]args){
for(int i=1;i<=100;i++){
if ((i%3==0)&&(i%5==0)){
System.out.print("ApaBoleh,");
}
System.out.print(i+",");
}
}
}
Then if i = 3
it'll evaluate to false
because it'll evaluate i%3==0
as true
and then go on to evaluate i%5==0
as false
( true
and false
= false
).
Then if i = 15
it'll evaluate to true
because it'll evaluate i%3==0
as true
and then go on to evaluate i%5==0
as true
( true
and true
= false
).
The conditions inside your }else if ((i%3==0)&&(i%5==0)){
evaluates as true whenever i%3 is equal to 0 or i%5 is equal to 0 so, if you really want }else if ((i%3==0)&&(i%5==0)){
to run you could either use }if ((i%3==0)&&(i%5==0)){
instead of it or you should at least place it above }else if (i%5==0){
or you could use it like this like the others told before me:
public class ApaBoleh{
public static void main(String[]args){
for(int i=1;i<=100;i++){
if ((i%3==0)&&(i%5==0)){
System.out.print("ApaBoleh,");
}else if (i%5==0){
System.out.print("Boleh,");
}else if(i%3==0){
System.out.print("Apa,");
}
System.out.print(i+",");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.