-
If else error
I wanna do the code as i is divisible by 3,5, or 7, then print foo, bar and baz. But it shows errors. Please advise. Thanks
public static void main(String[] args) {
int i;
for(i=1;i<=50;i++)
{
if(i%3=0)
{
System.out.println("Foo");
} else if (i%5=0)
{
System.out.println("Bar");
} else if (i%7=0)
{
System.out.println("Baz");
}
else
{
System.out.println("Wrong");
}
}
}
}
-
= is an assignment, you need == to compare int values.
And next time you post, use code tags and post the exact error message(s) you get.
Read the link in my signature.
-
I got it. Thanks for letting me know.
-
I wonder if it is possible to write this code using the switch stament?
And if it can be written, whether it is useful to write with switch stament?