One would think this life would be easier than it currently is. I mean come on wouldn’t ou think that? Of course you would. So why can’t we each believe in it I for one would want to believe in it. It would give me something to do with my day at least. But well there’s something else on my mind. We have to be able to understand what it is exactly, if we don’t? Then that’s something quite different now isn’t it? Yeah that’s what I was thinking.
Sometimes you want to be able to check if a number is odd or even. Here's a simple method that will do just that.
public class OddOrEven {
public static void main(String[] args) {
int i = Integer.valueOf(args[0]);
System.out.println(oddOrEven(i));
}
public static String oddOrEven(int number) {
if (number %2 == 0) {
return "Even";
} else {
return "Odd";
}
}
}
Naturally you'll want to do some value checking for the number input at the command line. It could easily throw an InvalidNumberFormat exception. I think that's the exception that would be thrown.
Comments
Post a Comment