java流程控制

java流程控制

一. Scanner对象

java.util.Scanner是java5的新特性.可以通过Scanner类获取用户的输入

Scanner s = new Scanner(System.in);

通过Scanner类的next()与nextLine0方法获取输入的字符串,在读取前我们一般需要使
用hasNext()与hasNextLine()判断是否还有输入的数据.

1.1 next();

  1. 一定要读取到有效字符后才可以结束输入
  2. 对输入的有效字符之前的空白,next()方法会自动将其去掉
  3. 只有输入有效字符才能将其后面输入的空白作为分隔符或者结束符
  4. next()不能得到带有空格的字符串
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        //判断用户有没有输入字符串
        if (scanner.hasNext()) {
            //使用next接收数据
            String str = scanner.next();
            System.out.println("输入的内容为"+str);
        }
        //凡是属于io流的如果不关闭会一直占用资源,要养成好习惯
            scanner.close();

1.2 nextLine();

  1. 以Enter为结束符,也就是说nextLine方法返回的是输入回车之前的所有字符.
  2. 可以获得空白
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("使用nextline方式接收:");
        if (scanner1.hasNextLine()) {
            //使用next接收数据
            String str = scanner1.nextLine();
            System.out.println("输入的内容为"+str);
        }
    
        scanner1.close();


二. 顺序结构

  • java的基本结构就是顺序结构,除非具体指明,否则就按照顺序一句一句的执行.
  • 顺序结构是最简单的算法结构
  • 语句与语句之间,框与框之间是按照从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的的一种基本算法结构

三. 选择结构

3.1 if单选择结构

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容:");
        String s=scanner.nextLine();
        //equals用于判断字符串是否一致
        if (s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();

3.2 if双选择结构

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入分数:");
         int s=scanner.nextInt();
        //equals用于判断字符串是否一致
        if (s>=60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }
        System.out.println("end");
        scanner.close();

3.3 if多选择结构

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入分数:");
        int s=scanner.nextInt();

        if (s==100){
            System.out.println("满分");
        }else if(s<100&&s>=80) {
            System.out.println("优秀");
        }else if(s<80&&s>=60){
            System.out.println("及格");
        }else if(s>=0&&s<60){
            System.out.println("不及格");
        }else{
            System.out.println("输入不合法");
        }
        System.out.println("end");
        scanner.close();

3.4 if嵌套结构

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入分数:");
        int s = scanner.nextInt();
        if (s > 0) {
            if (s > 10) {
                if (s > 30) {
                    System.out.println("s>30");
                }
                System.out.println("s>10");
            }
            System.out.println("s>0");
        }

3.5 switch多选择结构

  • 多选择结构还有一个实现方式就是switch case语句。
  • switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
  • switch语句中的变量类型可以是:
    1. byte, short, int 或者char,
    2. 从Java SE 7开始
    3. switch 支持字符串String类型了
    4. 同时case标签必须为字符串常量或字面量。
        //switch匹配char
        //case穿透 
        //switch匹配一个具体的值
        char grade = 'C';
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("不及格");
                break;
            default:
                System.out.println("错误");

        //switch匹配Spring字符串
        //字符的本质是数字
        //反编译 java--class(字节码文件)---反编译(idea)
        String s="杨晨阳";
        switch (s){
            case "杨晨阳":
                System.out.println("1");
                break;
            case "李连杰":
                System.out.println("2");
                break;
            case "甄子丹":
                System.out.println("3");
                break;
            case "吴京":
                System.out.println("4");
                break;
            default:
                System.out.println("错误");

反编译方法:

  1. 利用反编译工具
  2. 从目录中复制.class文件到.java文件目录中idea支持反编译

从反编译代码中可以看出java SE 7 中switch支持的spring类型本质还是数字

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

    public static void main(String[] args) {
        String s = "杨晨阳";
        byte var3 = -1;
        switch(s.hashCode()) {
        case 688376:
            if (s.equals("吴京")) {
                var3 = 3;
            }
            break;
        case 26290739:
            if (s.equals("杨晨阳")) {
                var3 = 0;
            }
            break;
        case 26582816:
            if (s.equals("李连杰")) {
                var3 = 1;
            }
            break;
        case 29532397:
            if (s.equals("甄子丹")) {
                var3 = 2;
            }
        }

        switch(var3) {
        case 0:
            System.out.println("1");
            break;
        case 1:
            System.out.println("2");
            break;
        case 2:
            System.out.println("3");
            break;
        case 3:
            System.out.println("4");
            break;
        default:
            System.out.println("错误");
        }
    }

四. 循环结构

  • while循环
  • do...while循环
  • for 循环
  • 在Java5中引入了一种主要用于数组的增强型for循环。

4.1 while循环

  • 只要布尔表达式为true,循环就会一直执行下去。
  • 我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
  • 少部分情况需要循环一直执行,比如服务器的请求向应监听等。
  • 循环条件一直为true就会造成无限循环死循环 ,我们正常的业务编程中应该尽量避免死循
    环。会影响程序性能或者造成程序卡死奔溃!
  • 思考 : 计算1+2+3+...+100=?
        //输出1-100
        int i = 0;
        int sum = 0;
        while (i <= 100) {
            sum += i++;
            System.out.println(sum);
        }

4.2 do...while

  • 对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,
    也至少执行一次。
  • do...while循环和while循环相似,不同的是, do...while循环至少会执行一次。
  • While和do-While的区别:
    1. while先判断后执行。dowhile是先执行后判断!
    2. D0..while总是保证循环体会被至少执行一次! 这是他们的主要差别。
        //输出1-100
        int i = 0;
        int sum = 0;
        do {
            sum += i++;
            System.out.println(sum);
        }while(i<=100);

4.3 for循环

  • 虽然所有循环结构都可以用while或者do...while表示,但Java提供了另- -种语句一for
    循环,使一些循环结构变得更加简单。

  • for循环语句是支持迭代的一-种通用结构,是最有效、最灵活的循环结构。

  • for循环执行的次数是在执行前就确定的。

    1. 练习1:计算0到100之间的奇数和偶数的和
    2. 练习2:用while或for循环输出1-1 000之间能被5整除的数,并且每行输出3个
    3. 练习3:打印九九乘法表
        //练习一
        int odds=0;
        int even=0;
        for (int i = 1; i <= 100; i++) {
            if(i%2==0){
                odds+=i;
            }else{
                even+=i;
            }
        }
        System.out.println(odds);
        System.out.println(even);
        System.out.println(odds + even);

        //练习二
        int j=0;
        for (int i = 1; i <=1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
                j++;
                if(i%3==0){
                    System.out.println();
                }
            }
        }

        //练习三
         for (int i = 1; i <= 9; i++) {
            for (int i1 = 1; i1 <= i; i1++) {
                System.out.print(i+"*"+i1+"="+i*i1+"\t");
            }
            System.out.println();
         }

4.4 增强for循环

  • Java5引入了-种主要用于数组或集合的增强型for循环。
  • 声明语句:声明新的局部变量.该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
  • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
        int[] numbers={10,20,30,40,50};

        for (int x:numbers) {
            System.out.println(x);
        }

4.5 break continue goto

  • break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,
    不执行循环中剩余的语句。(break语句也在 switch语句中使用)
        int i=0;
        while(i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
  • continue 语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,
    接着进行下一次是否执行循环的判定。
        int i=0;
        while(i<100){
            i++;
            if (i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i+"\t");
        }
  • 关于goto关键字
    1. goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用; Java
      没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影 子---带标签的break和
      continue。
    2. "标签” 是指后面跟一个冒号的标识符,例如: label:
    3. 对Java来说唯一 用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另
      个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
        int count = 0;
        //不建议使用
        outer:
        for (int i = 101; i < 150; i++) {
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    continue outer;
                }
            }
            System.out.print(i+"\t");
        }

打印三角形

        //打印三角形 5行
        for (int i = 1; i <= 5; i++) {
            for (int i1 = 5; i1 >= i; i1--) {
                System.out.print(" ");
            }
            for (int i2 = 1; i2 <= i; i2++) {
                System.out.print("+");
            }
            for (int i3 = 1; i3 < i; i3++) {
                System.out.print("+");
            }
            System.out.println();
        }

posted on 2020-10-01 19:00  桌角是小黑  阅读(145)  评论(0)    收藏  举报