Java流控制

一、Scanner 对象

作用:我们可以通过Scanner来获取用户的输入

在这里插入图片描述
使用 hasNext() 接收:

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //穿件一个扫描器对象,用于接收用户输入
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方法接收:");
        //判断用户是否进行了输入
        if (scanner.hasNext()) {
            //接收用户输入的信息
            String str = scanner.next();
            System.out.println("输入的内容为:" + str);
        }
        //用完scanner要关掉,节省资源
        //凡是属于IO流的使用完一定要关闭,防止占用资源
        scanner.close();
    }
}

当输入包含空格的字符串时:
在这里插入图片描述

使用 hasNextLine() 接收:

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用hasNextLine方法接收:");
        //判断是否还有输入
        if (scanner.hasNextLine()) {
            String str = scanner.nextLine();
            System.out.println("输入的内容为:" + str);
        }
        scanner.close();
    }
}

同样输入包含空格的字符串:
在这里插入图片描述

区别:
在这里插入图片描述
Next() 会把字符串中的空格作为结束符
NextLine() 是以回车为结束符

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        System.out.println("输入的内容为:" + str);
        scanner.close();
    }
}  

判断输入的是否为整数 int
hsaNextInt() 方法

import java.util.Scanner;

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

        int i = 0;

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数");
        if (scanner.hasNextInt()) {
            i = scanner.nextInt();
            System.out.println("你输入了:" + i);
        } else {
            System.out.println("抱歉,你输入的不是一个整数。。");
        }
        scanner.close();
    }
}

判断输入的是否为整数 float
hsaNextfloat() 方法

import java.util.Scanner;

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

     	float f = 0.0F;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个数");
        if (scanner.hasNextFloat()) {
            f = scanner.nextFloat();
            System.out.println("你输入了一个Float型数据");
            System.out.println("你输入了:" + f);
        } else {
            System.out.println("抱歉,你输入的不是一个小数。。");
        }
        scanner.close();
    }
}

运用实例小demo:

import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        //输入一组数字,并求出和和平均数,如果数据类非数字或者其他数据就显示出结果
        Scanner scanner = new Scanner(System.in);
        double sum = 0.0;
        int i = 0;
        int a = 2;//这个变量用来判断输入了第几个数字,并给用户提示信息
        System.out.println("请输入第1个数字:");
        //判断输入的数字类型
        while (scanner.hasNextDouble()) {
        System.out.println("请输入第" + a + "个数字:");
            a++;
            sum = sum + scanner.nextDouble();
            i++;
        }
        System.out.println("输入的这组数字的和为:" + sum);
        System.out.println("输入的这组数字的平均值为:" + sum / i);

    }
}

二、顺序结构

在这里插入图片描述


三、选择结构

1. if 单选结构

在这里插入图片描述
在这里插入图片描述

   Scanner scanner=new Scanner(System.in);
        int a=scanner.nextInt();
        if (a>=60){
            System.out.println("及格");
        }

2. if 双选结构

在这里插入图片描述

在这里插入图片描述

   Scanner scanner=new Scanner(System.in);
        int a=scanner.nextInt();
        if (a>=60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }

3. if 多选结构

在这里插入图片描述在这里插入图片描述

   Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        if (a <= 100 && a >= 90) {
            System.out.println("优秀");
        } else if (a < 90 && a >= 70) {
            System.out.println("良好");
        } else if (a >= 60 && a < 70) {
            System.out.println("及格");
        } else if (a >= 0 && a < 60) {
            System.out.println("及格");
        } else {
            System.out.println("输入的成绩不符合规则");
        }

4. if 嵌套结构

在这里插入图片描述

5. switch 多选结构

在这里插入图片描述

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透。当switch中没有break时,switch就会把符合条件的语句和符合条件以下的语句全部输出
        
        char grade = 'A';
        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("成绩格式不合法");
        }
    }
}

当使用了 break:
在这里插入图片描述
当未使用 break:
在这里插入图片描述

switch 比较字符串:
在这里插入图片描述

public class SwitchDemo01 {
   public static void main(String[] args) {
       String name = "ZQY";

       switch (name) {
           case "ZQY":
               System.out.println("优");
               break;
           case "666":
               System.out.println("优秀");
               break;
           default:
               System.out.println("???");
       }
   }
}

识别字符串原理上还是对比字符编码:
学着秦疆老师进行反编译(使用 idea 反编译)
首先找到idea编译后的文件放在哪:
在这里插入图片描述

找到 switch 这个程序对应的 class 文件:
在这里插入图片描述
然后手动复制到项目文件夹中:
在这里插入图片描述
idea 中就有了(直接往idea里复制会失败):
在这里插入图片描述
查看对比:
在这里插入图片描述


四、循环结构

1. while 循环

在这里2311231图片描述

实例:

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出 1~100
        int a = 1;
        while (a <= 100) {
            System.out.println(a);
            a++;
        }
    }
}

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出 1~100 的和
        int a = 0;
        int b = 0;
        while (a <= 100) {
            b = b + a;
            a++;
        }
        System.out.println(b);
    }
}

2. do while 循环

在这里插入图片描述
在这里插入图片描述

public class DoWhileDemo01 {
    public static void main(String[] args) {
        //输出 1~100 的和
        int a = 0;
        int b = 0;
        do {
            b = b + a;
            a++;
        }while (a <= 100);
        System.out.println(b);
    }
}

3. for 循环

在这里插入图片描述

几个 for 循环实例:

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

        //输出 1~100 的和
        int sum=0;
        for (int i = 0; i <=100 ; i++) {
            sum=sum+i;
        }
        System.out.println(sum);
    }
}
//for 死循环
        for (; ;) {
            
        }
public class ForDemo01 {
    public static void main(String[] args) {

        //计算 1~100 奇数的和  偶数的和
        int q = 0;//存储奇数的和
        int o = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 2 == 0) {
                //能被2整除,表示偶数
                o += i;
            } else {
                q += i;
            }
        }
        System.out.println("1~100奇数和为:" + q);
        System.out.println("1~100偶数和为:" + o);

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

        //计算 1~1000 能被5整除的数字,3个一行
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println("\n");
            }
        }
    }
}

打印九九乘法表:

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

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

4. 增强版 for 循环

在这里插入图片描述
主要用于遍历数组:

public class ForDemo01 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5, 6};
        for (int x : a)
            System.out.println(x);
    }
}

输出结果:
在这里插入图片描述
原理:将数组 a 中的数据逐一赋给变量 x

5. break 和 continue

在这里插入图片描述

break 用于退出循环:

public class BreakDemo {
    public static void main(String[] args) {
        int i=0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==20){
                break;
            }
        }
    }
}

在这里插入图片描述

continue 终止某次循环:

public class ContinueDemo {
    public static void main(String[] args) {
       // i 能被10整除就跳过并换行
        int i=0;
        while (i<100){
            i++;
            if (i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i+"\t");
        }
    }
}

在这里插入5描述

6. goto 关键字(不建议使用,仅供了解)

java没有goto,但保留goto关键字。从break,continue这两个关键字身上,可以看出一些goto的影子

定义一个标签,continue 结束这次循环,进行下次循环的时候从标签处开始执行

public class GoToDemo {
    public static void main(String[] args) {
        //打印101-150之间的质数
        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.println(i+ "\t");
        }
    }
}

小练习:
使用for循环打印三角形:

public class ForDemo01 {
    public static void main(String[] args) {
        //打印三角形 5 行

        for (int i = 0; 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.print("\n");
        }
    }
}

视频教程链接

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

覅乆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值