黑马教程java学习day3

目录

  • 一、程序的三种执行顺序
    • 1.顺序结构:
    • 2.分支结构:if,switch
    • 3.循环结构:for,while,do-while
  • 二、关于循环的扩展
    • 1.死循环:
    • 2.嵌套循环:
  • 三、如何跳出循环
    • 1.break和continue:
  • 四、综合案例:

一、程序的三种执行顺序

1.顺序结构:

正常代码运行的顺序,从上到下。
案例:

    public static void cd1(){
        int a = 10;
        int b = 20;
        System.out.println(a-b);
    }

2.分支结构:if,switch

(1)if分支结构:
定义:根据条件的真或假,来决定执行某段代码。
格式:

if..; if..else..; if..else if ..else;

案例:

    public static void cd1(){
        int a = 10;
        int b = 20;
        if(a>b){
            System.out.println("a大于b");
        }
        if(a>b){
            System.out.println("a大于b");
        }else{
            System.out.println("a不大于b");
        }
        if(a>b){
            System.out.println("a大于b");
        }else if(a==b){
            System.out.println("a等于b");
        }
    }

(2)switch分支结构:
定义:是通过比较值是否相等,来决定执行那条分支。
格式:

    * switch (变量) {
    case1:
        // 执行语句块1
        break;
    case2:
        // 执行语句块2
        break;
    ...
    default:
        // 默认执行语句块
}

案例:

    public static void cd2(){
        System.out.println("请输入红绿灯的状态:");
        Scanner sc = new Scanner(System.in);
        String status = sc.next();
        switch (status){
            case "red":
                System.out.println("停止");
                break;
            case "yellow":
                System.out.println("Caution");
                break;
            case "green":
                System.out.println("Go");
                break;
            default:
               System.out.println("输入错误");
        }
    }

if、switch的比较:各自适合什么业务场景:

  • if在功能上远远强于switch。
  • 当前条件是区间的时候,建议使用if分支结构来实现。
  • 当条件是与一个一个的值比较的时候,建议用switch更合适(格式良好,性能较好,代码优雅)

注意:

  • 表达式类型只能是byte,short,int,char,String,不支持double,float,long
  • case输出的值不允许重复,且只能是字面量,不能是变量。
  • 正常使用switch的时候,不能忘记配写break,否则会有穿透现象。

请使用运行主函数调用:

public static void main(String[] args) {
        cd1();
        cd2();
    }

3.循环结构:for,while,do-while

(1)for循环
定义:控制一段代码反复执行很多次。
格式:

    for (初始化语句; 条件判断语句; 循环后的操作语句) {
     循环体
     }

案例:

    //一个for循环,打印三次hello
    public static void main1() {
        for (int i = 0; i < 3; i++) {
            System.out.println("hello");
        }
    }

(2)while循环:
定义:控制一段代码反复执行很多次。
格式:

while (条件判断语句) {
     循环体
     }

案例:

 public static void main2() {
        int i = 0;
        while (i < 3) {
            System.out.println("hello");
            i++;
        }
    }

什么时候用for,什么时候用while?

  • 功能上一样,for能解决的,while也能解决。
  • 知道循环几次使用for,不知道循环几次使用while。

(3)do…while循环:
定义:控制一段代码反复执行很多次。
格式:

do {
     循环体
     } while (条件判断语句);*/

案例:

    public static void main3() {
        int i = 0;
        do {
            System.out.println("hello");
            i++;
        } while (i < 3);
    }

小结:

  • for循环。while循环(先判断后执行);do。。while(先执行后判断)。
  • for循环,while循环的执行流程是一模一样的,功能上无区别,for能做的while也能做,反之亦然。
  • 使用规范:知道循环几次使用for,不知道循环几次使用while。
  • 其他区别:for循环中,控制循环的变量只在循环中使用。while循环中,控制循环的变量在循环后还可以继续使用。

二、关于循环的扩展

1.死循环:

定义:一直在运行,出不来。
案例:

//请给我用while,for,dowhile实现的死循环
public static void main4() {
    while (true) {
        System.out.println("hello");
    }
}
public static void main5() {
    for (; ; ) {
        System.out.println("hello");
    }
}
public static void main6() {
    do {
        System.out.println("hello");
    } while (true);
}

2.嵌套循环:

定义:一个循环里面还有循环
案例:

    //打印一个3*3的*
    public static void main7() {
        for (int i=0;i<3;i++){
            for (int j=0;j<3;j++){
                System.out.println("*");
            }
        }
    }
      //打印九九乘法表
    public static void main8() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + i * j + "\t");
            }
            System.out.println();
        }
    }

三、如何跳出循环

1.break和continue:

(1)break:
定义:跳出并结束当前所在循环的执行。
(2)continue
定义:用于跳出当前循环的当前执行,直接进入循环的下一次执行。只能在循环中使用。
案例:

 public static void main9() {
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println(i);//0,1,2,3,4
        }
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                continue;
            }
            System.out.println(i);//0,1,2,3,4,6,7,8,9
        }
    }

四、综合案例:

案例1:

  //综合案例简单计算器
    public static void main10() {
        Scanner in=new Scanner(System.in);
        System.out.println("请输入第一个数字");
        double a=in.nextDouble();
        System.out.println("请输入第二个数字");
        double b=in.nextDouble();
        System.out.println("请输入运算符(+,-,/,*)");
        String c=in.next();
        switch (c){
            case "+":
                System.out.println(a+b);
                break;
            case "-":
                System.out.println(a-b);
                break;
            case "*":
                System.out.println(a*b);
                break;
            case "/":
                System.out.println(a/b);
                break;
            default:
                System.out.println("输入错误");
        }
    }

综合案例2:

  //猜数字游戏
    public static void main11() {
        Scanner in=new Scanner(System.in);
        int a=(int)(Math.random()*100)+1;//[0,99]+1=>[1,100]
        /*Random r=new Random()
        * int x=r.nextInt(100)+1
        * 随机生成的不同方式*/
        while (true){
            System.out.println("请输入数字");
            int b=in.nextInt();
            if (a==b){
                System.out.println("恭喜你猜对了");
                break;
            }
            if (a>b){
                System.out.println("猜小了");
            }else {
                System.out.println("猜大了");
            }
        }
    }

综合案例3:

   //验证码:开发一个程序,可以生成指定位数的验证码,每位都可以是数字,大小写字母
    public static void main12() {
        String code="";
        Scanner  in=new Scanner(System.in);
        System.out.println("请输入验证码位数");
        int n=in.nextInt();
        for (int i = 0; i < n; i++) {
            int a=(int)(Math.random()*3);
            //使用switch判断当前位置的字符类型
            switch (a){
                case 0:
                    code+=(char)(Math.random()*26+'a');
                    break;
                case 1:
                    code+=(char)(Math.random()*26+'A');
                    break;
                case 2:
                    code+=(int)(Math.random()*10);
                    break;
            }
        }
        System.out.println(code);
    }
}

提醒:请使用运行主函数调用。

你可能感兴趣的:(java_base,java,学习,python)