Java If-else语句



Java If-else语句

Java
if语句它检查布尔值条件:
true
false 。 Java中有多种类型的if语句。

if语句
if-else 语句
if-else-if 语句
嵌套if 语句

Java if语句

Java if语句测试条件。如果条件为真,它将执行
if块
语法:
 if(condition){
//code to be executed}

Java If-else语句_https://bianchenghao6.com_【Java 基础教程】_第1张

示例:
 //Java Program to demonstate the use of if statement.public class IfExample {
    public static void main(String[] args) {
        //defining an 'age' variable int age=20;
        //checking the age if(age>
        18){
            System.out.print("Age is greater than 18");
        }
    }
}

输出:
 Age is greater than 18 

Java if-else语句

Java if-else语句也测试条件。如果条件为true,则执行
if块,否则执行
else块
语法:
 if(condition){
//code if condition is true}
else{
//code if condition is false}

Java If-else语句_https://bianchenghao6.com_【Java 基础教程】_第2张

示例:
 //A Java Program to demonstrate the use of if-else statement.
//It is a program of odd and even number.public class IfElseExample {
    public static void main(String[] args) {
        //defining a variable int number=13;
        //Check if the number is divisible by 2 or not if(number%2==0){
            System.out.println("even number");
        }
        else{
            System.out.println("odd number");
        }
    }
}

输出:
 odd number 

Le年示例:
如果年份可以被4和400整除,则是leap年,但不能被100整除。
 public class LeapYearExample {
    public static void main(String[] args) {
        int year=2020;
        if(((year % 4 ==0) &
        &
        (year % 100 !=0)) || (year % 400==0)){
            System.out.println("LEAP YEAR");
        }
        else{
            System.out.println("COMMON YEAR");
        }
    }
}

输出:
 LEAP YEAR 

使用三元运算符

我们还可以使用三元运算符(?:)来执行if ... else语句的任务。这是检查情况的简便方法。如果条件为真,则结果为?返回。但是,如果条件为假,则返回: 的结果。
示例:
 public class IfElseTernaryExample {
    public static void main(String[] args) {
        int number=13;
        //Using ternary operator String output=(number%2==0)?"even number":"odd number";
        System.out.println(output);
    }
}

输出:
 odd number 

Java if-else-if阶梯语句

if-else-if阶梯语句从多个语句中执行一个条件。
语法:
 if(condition1){
//code to be executed if condition1 is true}
else if(condition2){
//code to be executed if condition2 is true}
else if(condition3){
//code to be executed if condition3 is true}
...else{
//code to be executed if all the conditions are false}

Java If-else语句_https://bianchenghao6.com_【Java 基础教程】_第3张

示例:
 //Java Program to demonstrate the use of if else-if ladder.
//It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.public class IfElseIfExample {
    public static void main(String[] args) {
        int marks=65;
        if(marks<50){
            System.out.println("fail");
        }
        else if(marks>=50 &&marks<60){
            System.out.println("D grade");
        }
        else if(marks>=60 &&marks<70){
            System.out.println("C grade");
        }
        else if(marks>=70 &&marks<80){
            System.out.println("B grade");
        }
        else if(marks>=80 &&marks<90){
            System.out.println("A grade");
        }
        else if(marks>=90 &&marks<100){
            System.out.println("A+ grade");
        }
        else{
            System.out.println("Invalid!");
        }
    }
}

输出:
 C grade 

检查正,负或零的程序:
 public class PositiveNegativeExample {
    public static void main(String[] args) {
        int number=-13;
        if(number>
        0){
            System.out.println("POSITIVE");
        }
        else if(number<
        0){
            System.out.println("NEGATIVE");
        }
        else{
            System.out.println("ZERO");
        }
    }
}

输出:
 NEGATIVE 

Java嵌套的if语句

嵌套的if语句表示另一个if块中的
if块。在此,仅当外部if块条件为true时才执行内部if块条件。
语法:
 if(condition){
    //code to be executed if(condition){
    //code to be executed }
}

Java If-else语句_https://bianchenghao6.com_【Java 基础教程】_第4张

示例:
 //Java Program to demonstrate the use of Nested if Statement.public class JavaNestedIfExample {
    public static void main(String[] args) {
        //Creating two variables for age and weight int age=20;
        int weight=80;
        //applying condition on age and weight if(age>
        =18){
            if(weight>
            50){
                System.out.println("You are eligible to donate blood");
            }
        }
    }
}

输出:
 You are eligible to donate blood 

示例2:
 //Java Program to demonstrate the use of Nested if Statement. public class JavaNestedIfExample2 {
    public static void main(String[] args) {
        //Creating two variables for age and weight int age=25;
        int weight=48;
        //applying condition on age and weight if(age>=18){
            if(weight>50){
                System.out.println("You are eligible to donate blood");
            }
            else{
                System.out.println("You are not eligible to donate blood");
            }
        }
        else{
            System.out.println("Age must be greater than 18");
        }
    }
}

输出:
 You are not eligible to donate blood