3种方式判断整数奇偶,其中2种负数时也可以判断

3种方式判断整数奇偶,其中2种负数时也可以判断,第1张

3种方式判断整数奇偶,其中2种负数时也可以判断
package test;

import java.util.Scanner;

public class Test28 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //只能判断正整数奇偶,负数都显示偶数
        method1(scanner);
        //负数也可以判断奇偶
        //method2(scanner);
        //负数也可以判断奇偶,位 *** 作,速度最快
        //method3(scanner);
    }

    private static void method1(Scanner scanner) {
        while (true) {
            System.out.println("method 1 ");
            System.out.println("INPUT NUMBER : ");
            int input = scanner.nextInt();
            if (input % 2 == 1) {
                System.out.println("奇数");
            } else {
                System.out.println("偶数");
            }
        }
    }

    private static void method2(Scanner scanner) {
        while (true) {
            System.out.println("method 2 ");
            System.out.println("INPUT NUMBER : ");
            int input = scanner.nextInt();
            if (input % 2 != 0) {
                System.out.println("奇数");
            } else {
                System.out.println("偶数");
            }
        }
    }

    private static void method3(Scanner scanner) {
        while (true) {
            System.out.println("method 3 ");
            System.out.println("INPUT NUMBER : ");
            int input = scanner.nextInt();
            if ((input & 1) == 1) {
                System.out.println("奇数");
            } else {
                System.out.println("偶数");
            }
        }
    }
}




如果method1不做任何处理,则

private static void method1(Scanner scanner) {
        while (true) {
            System.out.println("method 1 ");
            System.out.println("INPUT NUMBER : ");
            int input = scanner.nextInt();
            if (input % 2 == 1) {
                System.out.println("奇数");
            }
            if (input % 2 == 0) {
                System.out.println("偶数");
            }
        }
    }


当输入0时结束判断,进入下一个方法,0的奇偶判断也会做的,3个方法循环判断

package test;

import java.util.Scanner;

public class Test28 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //只能判断正整数奇偶,负数都显示偶数
        method1(scanner);
        //负数也可以判断奇偶
         method2(scanner);
        //负数也可以判断奇偶,位 *** 作,速度最快
        method3(scanner);
    }

    private static void method1(Scanner scanner) {
        while (true) {
            System.out.println("method 1 ");
            System.out.println("INPUT NUMBER : ");
            int input = scanner.nextInt();
            if (input % 2 == 1) {
                System.out.println("奇数");
            }
            if (input % 2 == 0) {
                System.out.println("偶数");
            }
            if (input==0){
                break;
            }
        }
    }

    private static void method2(Scanner scanner) {
        while (true) {
            System.out.println("method 2 ");
            System.out.println("INPUT NUMBER : ");
            int input = scanner.nextInt();
            if (input % 2 != 0) {
                System.out.println("奇数");
            } else {
                System.out.println("偶数");
            }
            if (input==0){
                break;
            }
        }
    }

    private static void method3(Scanner scanner) {
        while (true) {
            System.out.println("method 3 ");
            System.out.println("INPUT NUMBER : ");
            int input = scanner.nextInt();
            if ((input & 1) == 1) {
                System.out.println("奇数");
            } else {
                System.out.println("偶数");
            }
            if (input==0){
                break;
            }
        }
    }


}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5684949.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存