仅接受 2 作为数字并给出奇数或偶数的错误输出

Only Accepting 2 as a number and giving wrong output of odd or even

早上好, 我正在尝试自己学习一些东西。因为我已经超负荷工作了。这只是我正在研究的一个小练习项目。我明白这一点,但是,我不确定发生了什么。 当给出 2 以外的数字时,它会显示 "Please input a valid number, Thank you" 当给定 2 它说 "The Number is not even so there are Remainders" 我不确定为什么我会得到这个。为什么它不接受其他数字,为什么说 2 不是偶数? 对我解释错误的任何帮助将不胜感激。谢谢。

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

switch-case 的文档是 here .

现在试试这个,我在行中添加了一些注释。 仔细阅读

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();

    }//
}