18.java中判断(if…else) 顺序语句 判断(if…else)

语句:使用分号分隔的代码称作为一个语句。

18.java中判断(if…else)
顺序语句
判断(if…else)

注意:没有写任何代码只是一个分号的时候,也是一条语句,称作空语句。

18.java中判断(if…else)
顺序语句
判断(if…else)

顺序语句就是按照从上往下的顺序执行的语句。

判断(if…else)

       在我们找工作的过程中,要求两年工作经验以上且年龄超过30岁。

       什么是判断语句:用于判断的语句叫判断语句。

1.格式一

if(判断条件){
    如果符合条件执行的代码;
    执行的代码块1;
    执行的代码块2;
    ……………….;
    执行的代码块n;
}

18.java中判断(if…else)
顺序语句
判断(if…else)

练习:提示用户输入一个整数。如果该整数是5的倍数,打印“5的倍数”如果是2的倍数打印“2的倍数”

提示:为了便于让用户输入数据,我们使用Scanner这个类,固定用法Scanner sc=new Scanner(System.in); 该类需要导入包import java.util.Scanner;

int nextInt = sc.nextInt();获取用户输入的数字

import java.util.Scanner;
public class Demo9 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int nextInt = sc.nextInt();
        if(nextInt%5==0){
            System.out.println("是5的倍数");
        }
        if(nextInt%2==0){
            System.out.println("是2的倍数");
        }
    }
}

2.格式二

if(判断条件){
    执行的代码块1;
    执行的代码块2;
    ……………….;    
    执行的代码块n;
}else{
    执行的代码块1;
    执行的代码块2;
    ……………….;
    执行的代码块n;
}

18.java中判断(if…else)
顺序语句
判断(if…else)

案例:判断一个整数是奇数还是偶数

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int nextInt = sc.nextInt();
        if (nextInt % 2 == 0) {
            System.out.println("是偶数");
        } else {
            System.out.println("是奇数");
        }
        System.out.println("over");
    }

同样道理如果花括号中只有一条语句,那么花括号可以省略不写,初学者不推荐省略。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数:");
                int nextInt = sc.nextInt();
        if (nextInt % 2 == 0)
            System.out.println("是偶数");
        else
            System.out.println("是奇数");

        System.out.println("over");
    }

观察发现if else语句有点类似于三元运算符.其实三元运算符是if else 的一种简写格式.

Public static void main(String[] args) {
        int x = 0, y = 1, b;
        // if else 语句
        if (x > y) {
            b = x;
        } else {
            b = y;
        }
        System.out.println(b);// 1
        // 3元运算
        b = x > y ? x : y;
        System.out.println(b); // 1
}

这两种格式是一样的。if else 结构 简写格式: 变量 = (条件表达式)?表达式1:表达式2;

三元运算符:

好处:可以简化if else代码。

弊端:因为是一个运算符,所以运算完必须要有一个结果。

3格式三

if(判断条件1){
        执行的代码块1;
}else  if(判断条件2){
    执行语句;
}else if(判断条件3){
    执行语句;
}

需求: 根据用户定义的数值不同,打印对应的星期英文。if 只能进行一层判断,if else 只能进行两层判断,那么需要多层判断时呢?星期可是有7个数的。如何设计代码?

使用if 语句

public static void main(String[] args) {
        int x = 8;
        if (x == 1) {
            System.out.println("星期一");
        }
        if (x == 2) {
            System.out.println("星期二");
        }
        if (x == 3) {
            System.out.println("星期三");
        }
}

如果这样设计的话,第一个if语句执行完毕后,第二个语句仍会执行(去判断),是一个顺序结构.那么事实上当前定义的星期之后会有一个.假如,第一个已经符合条件,那么剩余的执行就没有意义了。属于逻辑错误。

使用if else ,如果用户输入的是7以外的数据,那么怎么处理?就需要使用else 了

方案2:使用if else if语句

public static void main(String[] args) {
        int x = 8;
        if (x == 1) {
            System.out.println("星期一");
        } else if (x == 2) {
            System.out.println("星期二");
        } else if (x == 3) {
            System.out.println("星期三");
        } else if (x == 4) {
            System.out.println("星期四");
        } else if (x == 5) {
            System.out.println("星期五");
        } else if (x == 6) {
            System.out.println("星期六");
        } else if (x == 7) {
            System.out.println("星期日");
        } else {
            System.out.println("请输入数字1-7");
        }
}

注意:

Public static void main(String[] args) {
        int x = 5;
        if (x == 1) {
            System.out.println("1");
        }
        if (x == 2) {
            System.out.println("2");
        }
        if (x == 3) {
            System.out.println("3");
        } else {
            System.out.println("4"); // 4
        }
}

该if 语句不是一个整体,第一个if 是一个语句,第二个又是一个语句,最后的if else 又是一个语句。

if语句特点

  1. 第二种格式与三元运算符的区别:三元运算符运算完要有值出现。好处是:可以写在其他表达式中。
  2. 条件表达式无论写成什么样子,只看最终的结构是否是true 或者 false。

练习1: 根据用户输入的月份,打印出月份所属的季节.

练习2: 根据用户输入的成绩,进行评级,根据学生考试成绩划分ABCD  

练习1:

public static void main(String[] args) {
        int x = 1;
        if (x == 3) {
            System.out.println("spring");
        } else if (x == 4) {
            System.out.println("spring");
        }
    }
        

 仔细观察:发现if和else if要执行的语句是一样的,可不可以合并呢。当然是可以的。怎么合并?使用逻辑运算符,那么使用哪个逻辑运算符呢, &肯定不行。需要全部为真才为真,月份是不可能同时满足的 那么使用|连接符号即可。意思只要其中一个为真,就为真。另外可以使用短路功能。

public static void main(String[] args) {
        int x = 1;
        if (x == 3 || x == 4 || x == 5) {
            System.out.println("spring");
        } else if (x == 6 || x == 7 || x == 8) {
            System.out.println("Summer");

        } else if (x == 9 || x == 10 || x == 11) {
            System.out.println("autumn");
        } else {
            System.out.println("Winter");
        } else {
            System.out.println("月份不存在");
        }
    }

练习2

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入考试分数:");
        double score = sc.nextDouble();
        char grade;
        if (score >= 90.0)
            grade = 'A';
        else if (score >= 80.0)
            grade = 'B';
        else if (score >= 70.0)
            grade = 'C';
        else if (score >= 60.0)
            grade = 'D';
        else
            grade = 'F';
        System.out.println("你的成绩是:" + grade);

    }

  

If语句常见的错误

1.忘记必要的括号:如果代码块中只有一条语句的时候,可以省略花括号,但是当花括号将多条语句扩在一起时,花括号就不能在省略。

double radius = 4;
double area;
if (radius >= 0)
  area = radius * radius * 3.14;
System.out.println("The area " + " is " + area);
double radius = 4;
double area;
if (radius >= 0) {
   area = radius * radius * 3.14;
   System.out.println("The area " + " is " + area);
}

虽然代码一样多,但是第一个会编译报错(area没有出初始化),第二个正常运行。就是因为少了花括号。所以一定要仔细.

2.if语句后出现分号

double radius = 0;
double area;
if (radius > 0); {
  area = radius * radius * 3.14;
  System.out.println("The area " + " is " + area);
}

注意:这是一个逻辑错误,编译和运行都不会报错,只是不会出现想要的结果。

相当于判断符合条件后,执行一个空语句

double radius = 0;
        double area;
        if (radius > 0){}{
            area = radius * radius * 3.14;
            System.out.println("The area " + " is " + area);
        }

判断闰年

1:什么是闰年?可以被4整除不能被100整除,或者可以被400整除,那么这一年就是闰年(leap year)

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入年份:");

        int year = sc.nextInt();
        // 判断年份能否被4整除
        boolean isLeapYear = (year % 4 == 0);
        // 年份能被4整除,并且不能被100整除并且使用&&(and)
        isLeapYear = isLeapYear && (year % 100 != 0);
        // 年份或者能够被400整除
        isLeapYear = isLeapYear || (year % 400 == 0);
        if (isLeapYear) {
            System.out.println(year + "是闰年!");
        }
        // 简写格式;
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            System.out.println(year + "是闰年!");
        }
    }