• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
tyrantblue
博客园    首页    新随笔    联系   管理    订阅  订阅
Java学习笔记(二)java流程控制

学习笔记2

Java流程控制

这次也是跟着【狂神说Java】Java零基础学习视频通俗易懂继续学习的,持续学习,你我共勉。

一、 用户交互Scanner

1. Scanner对象

  • 之前学的基本语法没有实现程序和人的交互,Java提供给我们一个工具类 java.util.Scanner 来获取用户的输入。
  • 基本语法
Scanner s = new Scanner(System.in);
  • 通过Scanner 类的next() 与nextLine() 方法获取输入的字符串,读取前我们一般需要使用hasNext() 与hasNextLine() 判断是否还有输入的数据
  • next()
    1. 一定要读取有效字符后才能结束输入
    2. 对输入有效字符之前遇到的空白,next() 方法会自动将其去掉
    3. 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符
    4. next() 不能得到带有空格的字符串
package com.tyrantblue.scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);//Hello world

        System.out.println("使用next方式接收:");

        //判断用户有没有输入字符串
        if(scanner.hasNext()){
            String str = scanner.next();
            System.out.println("输出:" + str);//Hello
        }

        //凡是属于IO流的类如果不关闭会一直占用资源,要养成用完即关的习惯
        scanner.close();
    }
}

  • nextline()
    1. 以Enter为结束符,也就是说nextLine() 方法返回的是输入回车之间的所有字符
    2. 可以获得空白
package com.tyrantblue.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//接收键盘数据 Hello world
        System.out.print("输入:");
        if(scanner.hasNextLine()){//判断是否输入
            String str = scanner.nextLine();
            System.out.println("输出:"+ str);//Hello world
        }
        scanner.close();
    }
}

*可以不用判断是否输入空

package com.tyrantblue.scanner;
import java.util.Scanner;
public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        String str = scanner.nextLine();
        System.out.println("输出的内容为:"+str);
        scanner.close();
    }
}
  • 判断是整数还是小数
package com.tyrantblue.scanner;

import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //从键盘接收数据
        int i = 0;
        float f = 0.0F;

        System.out.println("请输入整数:");

        //如果
        if(scanner.hasNextInt()){//判断是否输入整型
            i = scanner.nextInt();
            System.out.println("整型数据:"+ i);

        } else {
            System.out.println("输入的不是整型数据!");

        }

        System.out.println("请输入浮点型数据:");
        if(scanner.hasNextFloat()){//判断是否输入浮点型
            f = scanner.nextFloat();
            System.out.println("浮点型数据:"+ f);

        } else {
            System.out.println("输入的不是浮点型数据!");

        }
        scanner.close();
    }
}

2. Scanner练习

我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果

package com.tyrantblue.scanner;

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        //我们可以输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并输出执行结果
        Scanner scanner = new Scanner(System.in);

        //和
        double sum = 0;
        //计算输入了多少个数
        int m = 0;
        System.out.println("请输入数字:");
        //通过循环来判断是否还有输入,并进行数据统计
        while(scanner.hasNextDouble()){
            double x = scanner.nextFloat();
            sum = sum+x;
            m++;

        }
        //输出总和
        System.out.println("总和为:"+sum);
        System.out.println("平均数为:"+sum/m);
		scanner.close();
    }
}

二、 顺序结构

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

public class ShunXuDemo01 {
    public static void main(String[] args) {
        //顺序输出
        System.out.println("第一");
        System.out.println("第二");
        System.out.println("第三");
        System.out.println("第四");
        System.out.println("第五");
        System.out.println("第六");
    }
}

三、选择结构

1. if单选择结构

  • 我们很多时候需要去判断一个东西是否可行,然后我们才去执行,这样一个过程在程序中用if语句来表示
  • 语法:
if(布尔表达式){
    //如果布尔表达式为true则执行此语句
}
  • 例:
package com.tyrantblue.struct;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        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();
    }
}

2. if双选择结构

  • 有时有两个判断的需求,使用if-else结构
  • 语法
if(布尔表达式){
    //如果布尔表达式的值为true
}else{
    //如果布尔表达式的值为false
}
  • 例:
package com.tyrantblue.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于等于六十就是及格,小于六十就不及格。
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if (score>=60) {
            System.out.println("及格");
        } else{
            System.out.println("不及格");
        }
        scanner.close();
    }
}

3. if多选择结构

  • 上述代码不太符合实际情况,可能存在多个情况,这时我们需要一个多选择结构
  • 语法
if(布尔表达式A){
    //A为true
}else if(布尔表达式B){
    //A为false且B为true
}else if(布尔表达式C){
    //A和B为false且C为true
}else{
    //A,B和C都为false
}
  • 例:
package com.tyrantblue.struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        //成绩分等级
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        int score = scanner.nextInt();

        if(score == 100){
            System.out.println("恭喜满分!");
        }else if(score<100&&score>=90){
            System.out.println("A级");
        }else if(score<90&&score>=80) {
            System.out.println("B级");
        }else if(score<80&&score>=60) {
            System.out.println("C级");
        }else if(score<60&&score>=0) {
            System.out.println("不及格");
        }else{
            System.out.println("不合法");
        }
        scanner.close();
    }
}

4. 嵌套的if结构

  • 使用嵌套的if···else语句是合法的。你可以在另一个if或者else if 语句中使用if或者else if语句。也可以像if语句一样嵌套else if···else
  • 语法:
if(布尔表达式A){
    //如果布尔表达式A为true
    if(布尔表达式B){
        //如果布尔表达式B为ture
    }
}
  • 算法思想

5. switch多选择结构

  • 多选择结构还有一个实现方式就是switch case语句
  • switch case语句判断一个变量和与一系列之中某个值是否相等,每个值称为一个分支。
  • 语法
switch(expression){
    case value :
        //语句
        break;
    case calue :
        //语句
        break;
    //你可以有任意数量的case语句
    default ://可选
        //语句
}
  • 例:
package com.tyrantblue.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //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;
            default :
                System.out.println("404 NOT FOUND");
                break;
        }
    }
}
  • 从JDK7开始支持String的比较
  • 例:
package com.tyrantblue.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "Tyrant";
        //JDK7的新特性,表达式结果可以是字符串
        //字符的本质还是数字

        //反编译 java---class(字节码文件)---反编译(IDEA)

        switch(name){
            case"tyrant":
                System.out.println("tyrant");
                break;
            case"Tyrant":
                System.out.println("Tyrant");
                break;
            default:
                System.out.println("搓");
                break;
        }
    }
}

  • 反编译

在idea中直接将生成的class文件拖入本地项目目录中,然后在idea中查看字节码文件

  • SwitchDemo02.class的反编译源码
public class SwitchDemo02 {
    public SwitchDemo02() {
    }

    public static void main(String[] args) {
        String name = "Tyrant";
        byte var3 = -1;
        switch(name.hashCode()) {
        case -1774879654:
            if (name.equals("Tyrant")) {
                var3 = 1;
            }
            break;
        case -858746822:
            if (name.equals("tyrant")) {
                var3 = 0;
            }
        }

        switch(var3) {
        case 0:
            System.out.println("tyrant");
            break;
        case 1:
            System.out.println("Tyrant");
            break;
        default:
            System.out.println("搓");
        }

    }
}

四、循环结构

1. while循环

  • while是最基本的循环,它的结构为:
While(布尔表达式){
    //循环内容
}
  • 只要布尔表达式为true,循环就会一直执行下去。
  • 我们大多数情况是会让循环停止下来的,我们需要一个让表达式时效的方式来结束循环
  • 少部分的情况需要循环一直执行,比如服务器的请求相应监听等
  • 循环条件一直为true就会造成无限循环——死循环,我们正常的业务流程中应该尽量避免死循环。会影响程序性能或者造成程序卡死崩溃
  • 例:
//正常循环
package com.tyrantblue.struct;

public class WhileDemo01 {
    public static void main(String[] args) {

        //输出1~100

        int i = 0;

        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}

//死循环伪代码
package com.tyrantblue.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while (true){
            //等待客户端连接
            //定时检查
            //。。。。。
        }
    }
}
  • 实例:1+···+100
package com.tyrantblue.struct;

public class WhileDemo03 {
    public static void main(String[] args) {

        //1加到100求和
        int sum = 0;//总数
        int i = 0;//次数
        //while循环相加
        while(i<100){
            i++;
            sum += i;
        }
        System.out.println("总数为:" +sum);
    }
}

2. do...while循环

  • 对于while语句来说,如果不满足条件就不能进入循环,但有时候我们需要即使不满足条件,也至少执行一次
  • do...while循环和while循环类似,不同的是,do...while循环至少会执行一次
  • 语法
do{
    //代码语句
}while(布尔表达式);
  • 例:
package com.tyrantblue.struct;

//1到100求和
//do while
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do{
            sum = sum + 1;
        }while(i<100);
        System.out.println(sum);
    }
}
  • while 和do while循环的区别:
    1. while先判断后执行。do while 是先执行后判断
    2. do while总是保证循环体至少会被执行一次
    3. 例:
package com.tyrantblue.struct;

//while 和do while 循环的区别
public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        while(a<0){
            System.out.println(a);//上面不输出
            a++;
        }
        System.out.println("============================");
        do {
            System.out.println(a);//下面会输出
            a++;
        }while(a<0);
    }
}

3. for循环

  • 虽然所有循环结构都可以用while或者do while表示,但java提供了另一种语句——for循环,使一些循环结构变得更简单
  • for循环语句是支持迭代的一种通用结构,是最有效最灵活的循环结构
  • for循环执行的次数是在执行前就确定的,语法格式如下
for(初始化; 布尔表达式; 更新){
   //代码语句
}
  • 例:
package com.tyrantblue.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1;//初始化条件

        while(a<=100){//条件判断
            a += 2;//迭代
            System.out.println(a);
        }

        System.out.println("While循环结束!");
        
        //初始化//条件判断//迭代
        //100.for   //IDEA快捷键
        for (int i = 1; i<=100; i++){
            System.out.println(i);
        }
    }
}
  • for循环的顺序:

    1. 最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
    2. 然后检测布尔表达式的值,如果为true,循环体被执行,如果为false,循环终止,开始执行循环体后面的语句。
    3. 执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
    4. 再次检测布尔表达式,循环执行上面的过程。
  • 实例1:计算0到100之间奇数和偶数的和

package com.tyrantblue.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        //计算0到100之间的奇数和偶数的和

        int oddSum = 0;
        int evenSum = 0;

        for (int i = 0; i <= 100; i++) {
            if(i%2 != 0){
                oddSum += i;
            } else {
                evenSum +=i;
            }
        }

        System.out.println("奇数的和:"+oddSum);
        System.out.println("偶数的和:"+evenSum);

    }
}
  • 实例2:用while或者for循环输出1到1000之间能被5整除的数,并且每行输出3个
package com.tyrantblue.struct;

public class ForDemo03 {
    public static void main(String[] args) {

        //用while或者for循环输出1-1000之间能被5整除的数,每行3个
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%15 == 0){
                System.out.println("");
                //System.out.print("\n");
            }
        }
    }
}
  • 实例3:打印九九乘法表
package com.tyrantblue.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        //打印九九乘法表
        for (int i = 1; i <= 9; i++) {

            for (int j = 1; j <= i; j++) {
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println("");
        }
    }
}

4. 增强for循环

  • 这里简单了解一下,是由JDK5引入的一种主要用于数组或集合的
  • 语法:
for(声明语句 : 表达式){
    //代码句子
}

*声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组的值相等。

*表达式:表达式是要访问的数组名,或者返回值为数组的方法。

  • 例:
package com.tyrantblue.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] number = {10,20,30,40,50};//定义了一个数组

        for(int i = 0; i<5;i++){
            System.out.println(number[i]);
        }

        System.out.println("=====================");
        
        //遍历数组的元素
        for(int x : number){
            System.out.println(x);
        }
    }
}

五、break, continue

  • break在任何循环语句的主体部分均可用,用于强行退出循环,不执行循环中剩余的语句。(break也在switch语句中使用)

  • continue用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

  • 例:

package com.tyrantblue.struct;

public class ContinueDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            if(i%10 == 0){
                System.out.println();
                continue;
            }
            System.out.println(i);
        }
    }
}

  • 关于goto关键字
    1. goto并未在java语言中得到正式使用,带标签的break和continue会中断到存在标签的位置。
    2. 标签是指后面跟一个冒号的标识符,例如:label:
    3. 目前只需要了解,不需要掌握

六、练习题

  • 打印三角形
package com.tyrantblue.struct;

public class TextDemo01 {
    public static void main(String[] args) {
        //打印三角形
        //打印空三角
        //打印实三角
        //打印实三角

        for (int i = 1; i <= 5; i++) {
            for(int j = 5;j>i;j--){
                System.out.print(" ");
            }
            for (int j = 1;j<=i;j++){
                System.out.print("*");
            }
            for (int j = 1;j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }

    }

posted on 2023-03-02 14:41  雪化凛然  阅读(38)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3