Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Find even/odd numbers using switch

807597Nov 14 2005 — edited Mar 29 2008
I'm writing a program that is suppose to ask the user for 20 integers. My job is to see if the integer is even or odd. I know how to do it with if statements but how do I do it using switch?

The only way I can think of right now is do the if statements first(if it's even, i'll set a string to "e" and if it's odd, set it to "o") then do the switch statement.
if (num % 2 == 0)
{
   answer = "e"
}
else if (num % 2 != 0)
{
   answer = "o"
}
switch (answer) {
   case "e": blahblahblah
   case "o": blah blah blah
}
I know this is a very stupid way of doing it. So is there any smarter way? And yes, I must use switch statements.

Thanks!!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 26 2008
Added on Nov 14 2005
17 comments
500 views