JAVA学习笔记三之(2)语句①

来源:互联网 发布:pes贝克汉姆捏脸数据 编辑:程序博客网 时间:2024/05/28 14:57

JAVA学习笔记三之(2)语句①

一、语句的分类

  1、顺序结构
    从上往下依次执行。
  
  2、选择结构
    if语句
    switch语句
    
  3、循环结构
    for
    while
    do……while

二、if语句

  1、if语句的格式一 
    (1)、格式:
      if(比较表达式) {
        被if语句控制的语句体;
      }
    (2)、执行流程:
      如果比较表达式的结果为true,就进入执行语句体。
      如果比较表达式的结果为false,就不进入,但是可以继续执行下面的语句。

class IfStatementDemo1 {    public static void main(String[] args) {        //需求:判断两个数据是否相等,如果是,就输出"数据相等"。        //定义两个数据        int a = 10;        int b = 20;        if (a == b) {            System.out.println("a相等b");        }        int c = 10;        if (a == c) {            System.out.println("a相等c");        }        System.out.println("结束");    }}

  2、if语句的注意事项:
    A:比较表达式无论简单还是复杂,结果必须是boolean类型。
    
    B:判断的时候,如果有常量,请把常量放在左边。
    
    C:if语句控制的语句体如果是一条语句,大括号可以省略。但是,如果是多条语句,就不能省略。
      建议:永远不要省略。
      
    D:不要在比较表达式的括号后面随意添加分号。
      一般来说:有左大括号就没有分号,有分号就没有左大括号。

class IfStatementDemo2 {    public static void main(String[] args) {        int a = 10;        int b = 20;        int c = 10;        if (a == b & a == c) {            System.out.println("好好学习");        }        System.out.println("------------");        //错误        /*        if (a = b) { //把b的值赋值给a,把a留下来。            System.out.println("天天向上");        }        */        boolean flag = false;        if (flag) {            System.out.println("我是true1");        }        System.out.println("------------");        if (flag == true) {            System.out.println("我是true2");        }        System.out.println("------------");        if (flag = true) {            System.out.println("我是true3");        }        //虽然上述的做法并没有问题,但是很多时候,我可能要做的真的是一个判断操作,而不是一个赋值操作。        //之所以出现上面的问题,是因为我写错了。        //怎么解决呢?        //只要是做判断,如果有变量还是常量。那么,请把常量写在左边。        /*        if (true = flag) {            System.out.println("我是true4");        }        */        System.out.println("------------");        int x = 10;        int y = 20;        if (x == y)            System.out.println("不相等1");        if (x == y) {            System.out.println("不相等2");            System.out.println("不相等3");        }        System.out.println("------------");        if (x == y); {            System.out.println("不相等2");            System.out.println("不相等3");        }    }}

  3、if语句的格式二
    (1)、格式:
      if(比较表达式) {
        语句体1;
      } else {
        语句体2;
      }
    (2)、执行流程:
      如果比较表达式的结果为true,就进入执行语句体1。
      如果比较表达式的结果为false,就进入执行语句体2。
    (3)、注意:
     if语句的第二种格式在某些情况下可以和三元运算符等价。

class IfStatementDemo3 {    public static void main(String[] args) {        //需求:判断两个数据是否相等。        int a = 10;        int b = 20;        if (a == b) {            System.out.println("a等于b");        }else {            System.out.println("a不等于b");        }        //需求:获取两个数据中的最大值。        int x = 100;        int y = 200;        if (x >= y) {            System.out.println(x);        }else {            System.out.println(y);        }    }}

  4、if语句在某些情况下可以和三元运算符等价转换。
    三元运算符的内容都可以用if语句替换。
    反之不成立。
    
    注意:
      当if语句控制的语句体是输出语句的时候,用三元运算符就没有办法改进。
      因为三元运算符是一个运算符,必须要有一个结果。

class IfStatementDemo4 {    public static void main(String[] args) {        //比较两个数据是否相等。        int a = 10;        int b = 20;        //三元运算符        boolean flag = (a == b) ? true : false;        System.out.println(flag);        //用if语句实现        boolean flag2;        if (a == b) {            flag2 = true;        }else {            flag2 = false;        }        System.out.println(flag2);        //获取两个数据中的最大值。        int x = 100;        int y = 200;        //三元运算符        int max = (x > y) ? x : y;        System.out.println(max);        //用if语句实现(在if语句体中仅仅是赋值,而不是输出)        int max2;        if (x > y) {            max2 = x;        }else {            max2 = y;        }        System.out.println(max2);        //需求:获取两个数据中的最大值改进版        if (x >= y) {            System.out.println(x);        }else {            System.out.println(y);        }        //请把上面的格式,用三元运算符改进        //int max3 = (x >= y) ? System.out.println(x) : System.out.println(y);    }}

  5、if语句的格式三和格式四
    (1)、if语句格式三:
      if (比较表达式1) {
        语句体1;
      }else if (比较表达式2) {
        语句体2;
      }else if (比较表达式3) {
        语句体3;
      }
      ……
      else if (比较表达式n) {
        语句体n;
      }

      执行流程:
        首先判断比较表达式1,看其返回值是:
          true:就进去执行语句体1;
          false:就继续判断比较表达式2,看其返回值是:
            true:就继续执行语句体2;
            false:就继续判断比较表达式3,看其返回值是:
              ……
        如果所有比较表达式都不成立,这个if语句体没有执行。可以继续向下执行。
        
    (2)、if语句格式四:
      if (比较表达式1) {
        语句体1;
      }else if (比较表达式2) {
        语句体2;
      }else if (比较表达式3) {
        语句体3;
      }
      ……
      else if (比较表达式n) {
        语句体n;
      }else {
        语句体n+1;
      }

      执行流程:
        首先判断比较表达式1,看其返回值是:
          true:就进去执行语句体1;
          false:就继续判断比较表达式2,看其返回值是:
            true:就继续执行语句体2;
            false:就继续判断比较表达式3,看其返回值是:
              ……
        如果所有比较表达式都不成立,执行语句体n+1。

import java.util.Scanner;class IfStatementDemo5 {    public static void main(String[] args) {        //根据学生的成绩,判断学生成绩所属的等级。        //90-100 优秀        //80-90 好        //70-80 良        //60-70 及格        //60以下 不及格        Scanner sc = new Scanner(System.in);        System.out.print("请输入一个成绩:");        int score = sc.nextInt();        //我们完全可以采用第一种格式来做,但是,那样的话,每种判断都会执行。        //所以,我们将采用第三种或者第四种方式来做。        //方式1 考虑的不够周全        /*        if (score > 90 && score <= 100) {            System.out.println("优秀");        }else if (score >= 80 && score < 90) {            System.out.println("好");        }else if (score >= 70 && score < 80) {            System.out.println("良");        }else if (score >= 60 && score < 70) {            System.out.println("及格");        }else if (score < 60) {            System.out.println("不及格");        }        */        //方式2 考虑周全了,但是,超过100的,或者小于0的,没有提示。所以,这个程序还是不好。        /*        if (score > 90 && score <= 100) {            System.out.println("优秀");        }else if (score >= 80 && score < 90) {            System.out.println("好");        }else if (score >= 70 && score < 80) {            System.out.println("良");        }else if (score >= 60 && score < 70) {            System.out.println("及格");        }else if (score >= 0 && score < 60) {            System.out.println("不及格");        }        */        //方式3        if (score > 90 && score <= 100) {            System.out.println("优秀");        }else if (score >= 80 && score < 90) {            System.out.println("好");        }else if (score >= 70 && score < 80) {            System.out.println("良");        }else if (score >= 60 && score < 70) {            System.out.println("及格");        }else if (score >= 0 && score < 60) {            System.out.println("不及格");        }else {            System.out.println("成绩输入有误");        }    }}

    (3)、注意:
      所有的判断条件出现都必须有对应的if。   

    (4)、if语句的第三种格式和第四种格式的区别在于:
      第三种的事没有其他情况。很多时候,可能需要进一步的处理。
      建议用第四种格式。     

    (5)、练习
      根据给定的x的值,来确定y的值。
      他们之间满足如下的关系:
        x>3 y=2x+1;
        3>x>-1 y=2x;
        x<=-1 y=2x-1;

import java.util.Scanner;class IfStatementDemo6 {    public static void main(String[] args) {        //键盘录入        Scanner sc = new Scanner(System.in);        //键盘录入数据        System.out.print("请输入一个int类型的值:");        int x = sc.nextInt();        //通过判断x的范围,来决定y的值        int y;        /*        //只要有if语句,编译器就认为可能不成功。        if (x >= 3) {            y = 2*x+1;        }else if (x > -1 && x < 3) {            y = 2*x;        }else if (x <= -1) {            y = 2*x-1;        }        */        if (x >= 3) {            y = 2*x+1;        }else if (x > -1 && x < 3) {            y = 2*x;        }else {            y = 2*x-1;        }        System.out.println("y的值是:" + y);    }}

  6、if语句总结
    一种判断:用第一种格式
    二种判断:用第二种格式
    三种以上的判断:用第三种格式或者第四种格式

    练习:根据输入的月份,判断季节。
    分析:
      春 3,4,5
      夏 6,7,8
      秋 9,10,11
      冬 12,1,2

import java.util.Scanner;class IfStatementDemo7 {    public static void main(String[] args) {        //键盘录入月份        Scanner sc = new Scanner(System.in);        //输入月份        System.out.println("请输入月份:");        int month = sc.nextInt();        //方式1        //这个代码也是没有问题的。但是代码量太大了。        if (month == 1) {            System.out.println("冬");        }else if (month == 2) {            System.out.println("冬");        }else if (month == 3) {            System.out.println("春");        }else if (month == 4) {            System.out.println("春");        }else if (month == 5) {            System.out.println("春");        }else if (month == 6) {            System.out.println("夏");        }else if (month == 7) {            System.out.println("夏");        }else if (month == 8) {            System.out.println("夏");        }else if (month == 9) {            System.out.println("秋");        }else if (month == 10) {            System.out.println("秋");        }else if (month == 11) {            System.out.println("秋");        }else if (month == 12) {            System.out.println("冬");        }else {            System.out.println("月份录入有误");        }        System.out.println("-----------------");        //方式2        //由于:month == 3, month == 4, month == 5的输出结果是一样的。        //所以,如果我们能够把这三个条件结合起来就好了。        //一般,我们合并boolean类型的条件用逻辑运算符。        //而逻辑运算符常用的:&&, ||, !        //通过简单的分析,我们选择了||。        if (month == 1 || month == 2 || month == 12) {            System.out.println("冬");        }else if (month == 3 || month == 4 || month == 5) {            System.out.println("春");        }else if (month == 6 || month == 7 || month == 8) {            System.out.println("夏");        }else if (month == 9 || month == 10 || month == 11) {            System.out.println("秋");        }else {            System.out.println("月份录入有误");        }        System.out.println("-----------------");        //这个时候,代码已经还可以了,但是呢,我们继续想。        //这个时候,如果我要大家做的是很多个数据的判断:        //比如说:上半年和下半年。        //我们继续发现:原来,这个判断还是在某个范围内的。        //所以,我们采用范围的判断解决        if (month < 1 || month > 12) {            System.out.println("月份输入有误");        }else if (month >= 3 && month <= 5) {            System.out.println("春");        }else if (month >= 6 && month <= 8) {            System.out.println("夏");        }else {            System.out.println("冬");        }    }}

  7、if语句的嵌套
    if语句的几种格式间是可以相互嵌套的。

    练习:
      判断三个数中的较大的值。
    分析:
      前面我们采用的是分解做法。即前两个比较,然后和第三个比较。
      但这种问题适合于if语句的嵌套使用。即使用if语句的第二种格式嵌套第二种格式。

class IfStatementDemo8 {    public static void main(String[] args) {        int a = 10;        int b = 20;        int c = 30;        if (a > b) {            //a            if (a>c) {                //a                System.out.println(a);            }else {                //c                System.out.println(c);            }        }else {            //b            if (b>c) {                //b                System.out.println(b);            }else {                //c                System.out.println(c);            }        }    }}

三、switch语句

  1、switch语句的格式
    switch (表达式) {
      case 值1:
      语句1;
      break;
    case 值2:
      语句2;
      break;
    case 值3:
      语句3;
      break;
    …
    default:
      语句n;
      break;
    }

  2、针对格式的解释:
    A:switch说明这是一个选择/判断结构的语句。
    B:switch后面的表达式的取值
      byte, short, int, char
      JDK5以后,可以是枚举类型(就业班讲)。
      JDK7以后,可以是String类型。
    C:case后面的值就是和表达式进行匹配的内容。
    D:default相当于if语句的else。
      当所有的值都不匹配的时候,它就执行。

  3、switch语句的执行流程:
    首先计算表达式的值。
    其次,进去依次和case的值进行匹配。
      如果有对应的选项:就执行该对应选择的语句。
      如果所有的选项都不匹配:就执行default。

import java.util.Scanner;class SwitchDemo {    public static void main(String[] args) {        //需求:我键盘录入1,2,3...,对应的输出:星期一,星期二,...        Scanner sc = new Scanner(System.in);        System.out.println("请输入一个1-7的数据:");        int number = sc.nextInt(); //4        //表达式可以是byte,short,int        switch (number) { //4            case 1:                System.out.println("星期一");                break;            case 2:                System.out.println("星期二");                break;            case 3:                System.out.println("星期三");                break;            case 4:                System.out.println("星期四");                break;            case 5:                System.out.println("星期五");                break;            case 6:                System.out.println("星期六");                break;            case 7:                System.out.println("星期日");                break;            default:                System.out.println("输入的星期有问题");                break;        }        System.out.println("----------------------------");        //表达式可以是char类型        //char choice = '女';        char choice = 'A';        switch (choice) {            case '男':                System.out.println("你是男的");                break;            case '女':                System.out.println("你是女的");                break;            default:                System.out.println("保密");                break;        }        System.out.println("----------------------------");        //表达式可以是String类型        String s = "hello";        switch (s) {            case "hello":                System.out.println("你选择了hello");                break;            case "world":                System.out.println("你选择了world");                    break;            case "java":                System.out.println("你选择了java");                 break;            default:                System.out.println("选择有误");                break;        }    }}

  4、使用switch语句需要注意的问题:
  
    A:case和default的顺序有没有什么要求?
      case和default的位置没有什么要求。但是建议按照基本格式做。
      在执行的时候,肯定是先匹配case,只有所有的case都不匹配的时候,才会去执行default。
      
    B:break可以省略吗?
      可以。但是结果可能不是你想要的。所以,建议不要省略。
      如果break省略了,当匹配某一个成功后,它不会停下来,会继续执行。这个现象被称为:case穿透。
      
    C:default如果在最后,break是可以省略的。
    
    D:switch语句的结束:
      a:遇到break。
      b:执行到程序的末尾。

import java.util.Scanner;class SwitchDemo2 {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.println("请输入一个1-7的数据:");        int number = sc.nextInt();         //位置关系        /*        switch (number) {            default:                System.out.println("输入的星期日期有问题");                break;            case 1:                System.out.println("星期一");                break;            case 2:                System.out.println("星期二");                break;            case 3:                System.out.println("星期三");                break;            case 4:                System.out.println("星期四");                break;            case 5:                System.out.println("星期五");                break;            case 6:                System.out.println("星期六");                break;            case 7:                System.out.println("星期日");                break;        }        */        //break的事情        switch (number) {            case 1:                System.out.println("星期一");                break;            case 2:                System.out.println("星期二");                break;            case 3:                System.out.println("星期三");                break;            case 4:                System.out.println("星期四");                break;            case 5:                System.out.println("星期五");                break;                      case 6:                System.out.println("星期六");                break;            case 7:                System.out.println("星期日");                break;            default:                System.out.println("输入的星期有问题");                //break;        }    }}

  5、看程序写结果。

class SwitchDemo3 {    public static void main(String[] args) {        ////第一种情况        int x = 4;        int y = 5;        switch (x) {            default:                y++;            case 1:                y++;            case 2:                y++;            case 3:                y++;        }        System.out.println(y); //9        //第二种情况        int a = 4;        int b = 5;        switch (a) {            default:                b++;            case 1:                b++;            case 2:                b++;            case 3:                b++;            case 4:                b++;        }        System.out.println(b); //6    }}
0 0
原创粉丝点击