Good morning, I'm trying to learn somethings on my own. As I'm overloaded for assignments as it is. This is just a small practice program I was working on. I get it to this point however, I'm unsure of what is happening. When given a number other than 2 it says "Please input a valid number, Thank you" When given 2 it says "The Number is not even so there are Remainders" I'm unsure why I'm getting this. Why is it not accepting other numbers and why is it saying 2 isn't even? Any help on what I'm interpreting wrong would be appreciated. Thank you.
import java.util.Scanner;
public class Assignment1
{
//Scanner keyboard = new Scanner(System.in);
//int num = keyboard.nextInt();
public static int isEven()
{
Scanner keyboard = new Scanner(System.in);
int num = keyboard.nextInt();
switch (num)
{
case 1:
if (num % 2 == 0)
System.out.println("The Number is Even no Remainders");
break;
case 2:
if (num % 2 != 0);
System.out.println("The Number is not even so there are Remainders");
break;
default:
System.out.println("Please input a valid number, Thank you.");
}//switch
/*pull number from user
//store in num
//if even print message num is even
//else print message not an even number
* This is the remainder of my psuedcode notes to remind me how my
* mind was flowing
*/
return num;
} //isEven
public static void main (String args[])
{
Assignment1.isEven();
}//main
}//public class assignment one
The documentation for switch-case is here .
And now try this, I added some comment to lines. Read those carefully.
public class Assignment1 {
public static int isEven() {
Scanner keyboard = new Scanner(System.in);
int num = keyboard.nextInt();
num = num % 2; // divide by 2 and get a remainder.
switch (num) {
case 0: //case 0 means if number equal to zero
System.out.println("The Number is Even no Remainders");
break;
case 1: // case 1 means if number equal to one
System.out.println("The Number is not even so there are Remainders");
break;
default: // if no one match if not a valid.
System.out.println("Please input a valid number, Thank you.");
}//switch
/*pull number from user
//store in num
//if even print message num is even
//else print message not an even number
* This is the remainder of my psuedcode notes to remind me how my
* mind was flowing
*/
return num;
} //isEven
public static void main(String args[]) {
Assignment1.isEven();
}//
}