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; } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)