java if 条件语句
import java.util.Scanner; public class Sample { public static void main(String[] args) { int num; Scanner ip = new Scanner(System.in); System.out.print("Enter any number: "); num = ip.nextInt(); if (num > 0) { System.out.println("Positive number"); } if (num < 0) { System.out.println("Negative number"); } if (num == 0) { System.out.println("Zero"); } ip.close(); } } OUTPUT: Enter any number: -25 Negative number
demo2
import java.util.Scanner; public class Sample { public static void main(String[] args) { int num; Scanner ip = new Scanner(System.in); System.out.print("Enter any number: "); num = ip.nextInt(); if (num > 0) System.out.println("Positive number"); else if (num < 0) System.out.println("Negative number"); else System.out.println("Zero"); ip.close(); } } OUTPUT: Enter any number: 0 Zero
demo3
import java.util.Scanner; public class Sample { public static void main(String[] args) { int num; Scanner ip = new Scanner(System.in); System.out.print("Enter any number: "); num = ip.nextInt(); if (num > 0) { System.out.println("Positive number"); if (num % 2 == 0) System.out.println("Even number"); else System.out.println("Odd number"); } else if (num < 0) { System.out.println("Negative number"); if (num % 2 == 0) System.out.println("Even number"); else System.out.println("Odd number"); } else System.out.println("Zero"); ip.close(); } } OUTPUT: Enter any number: -24 Negative number Even number