Day3 Java流程控制(Scanner、三种结构、break和continue)

Java流程控制

跟狂神学的!强推!!
https://www.bilibili.com/video/BV12J41137hu?spm_id_from=333.999.0.0

用户交互Scanner

scanner对象
之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。
基本语法:
scanner s = new Scanner(System.in);
通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。

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()){
            //使用next方式接收
            String str =scanner.next();
            System.out.println("输入的内容为:"+str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();

    }
}
/*
使用next方式接收:
Hello World!
输入的内容为:Hello  注意只有空白格前面的数据显示出来
*/

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        //判断用户有没有输入字符串
        if (scanner.hasNextLine()){
            //使用nextLine方式接收
            String str = scanner.nextLine();
            System.out.println("输入的内容为:"+str);
        }
        scanner.close();
    }
}
/*
使用next方式接收:
Hello World
输入的内容为:Hello World
*/

next():
1、一定要读取到有效字符后才可以结束输入。

2、对输入有效字符之前遇到的空白,next()方法会自动将其去掉。

3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。

4、next()不能得到带有空格的字符串。

nextLine():
1、以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。

2、可以获得空白。

补充知识

import java.util.Scanner;

public class Demo03 {
    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();

    }
    /*
    请输入整数:
10
整数数据:10
请输入整数:
0.1
小数数据:0.1
    */
    //示例
    import java.util.Scanner;

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

        //sum
        double sum =0;
        //计算输入多少个数字
        int number =0;
        //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
        while (scanner.hasNextDouble()){
            double x =scanner.nextDouble();
            number++;
            sum = sum +x;
            System.out.println("你输入了第"+ number +"个数据,当前sum结果为:" + sum);
        }
        System.out.println(number + "个数的和为" +sum);
        System.out.println(number + "个数的平均值为" +(sum/number));
        scanner.close();
    }
}
    /*
10
你输入了第1个数据,当前sum结果为:10.0
20
你输入了第2个数据,当前sum结果为:30.0
30
你输入了第3个数据,当前sum结果为:60.0
z
3个数的和为60.0
3个数的平均值为20.0

    */

结构

顺序结构

JAVA的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行。顺序结构是最简单的算法结构。

语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。

public class ShunDemo {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}
/*
hello1
hello2
hello3
hello4
hello5
*/

选择结构

1.if单选择结构

在这里插入图片描述

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scannar = new Scanner(System.in);
        System.out.println("请输入内容:");
        String str = scannar.nextLine();
        //equals:判断字符串是否相等
        if(str.equals("Hello")){
            System.out.println(str);
        }
        System.out.println("End");
        scannar.close();
    }
}
/*
请输入内容:
hi!
End

请输入内容:
Hello
Hello
End
*/

2.if双选择结构
在这里插入图片描述

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于60就是及格,小于60就不及格
        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();

    }
}
/*
请输入成绩:
60
及格

请输入成绩:
59
不及格
*/

3.if多选择结构

在这里插入图片描述

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>=70){
            System.out.println("C级");
        }
        else if(score<70&&score>=60){
            System.out.println("D级");
        }
        else if(score<60&&score>=0){
            System.out.println("不及格");
        }
        else{
            System.out.println("成绩不合法");
        }

        scanner.close();
    }
}
/*
请输入成绩:
111
成绩不合法

请输入成绩:
66
D级
*/

4.嵌套的if结构

5.switch多选择结构

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade ='A';
        //case穿透现象 //switch 匹配一个具体的值
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;//可选,最好每个case跟着一个break;
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("合格");
            case 'D':
                System.out.println("及格");
            case 'E':
                System.out.println("挂科");
            default:
                System.out.println("未知等级");
        }
    }
}
/*
优秀

若grade为B,执行结果:
良好
合格
及格
挂科
未知等级
*/
//示例
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "qqq";
        switch (name){
            case "qqq":
                System.out.println("welcome:"+name);
            case "yyy":
                System.out.println("welcome:"+name+"yyy");
            case "QYH":
                System.out.println("welcome:"+name+"QYH");
                break;
            case "666":
                System.out.println("welcome:"+name);
            default:
                System.out.println("Can't find:"+name);
        }
    }
}
/*
welcome:qqq
welcome:qqqyyy
welcome:qqqQYH
*/

学会查看class文件

为了理解字符串本质上还是数字这一句话,我们需要比对字节码文件和java文件。

打开file选择project structure,把project compiler output下的路径复制,在文件查看器中打开,把.class文件复制,然后右键点击package,选择open in explorer,在文件夹中粘贴.class文件,idea就会显示出.class文件。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1eu5NYY3-1636022353816)(C:\Users\86183\Desktop\study\java\note\photos\QQ图片20211104150736.png)]

循环结构

1.while循环

只要布尔表达式为true,循环就会一直执行下去。
我们大多数情况是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。少部分情况需要循环一直执行,比如服务器的请求响应监听等。
循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应该尽量避免死循环。会影响程序性能或者造成程序卡死奔溃!

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

思考:计算1+2+3+…+100=?

public class WhileDemo02 {
    public static void main(String[] args) {
        //输出1~100相加的值
        int i =0;
        int sum=0;
        while(i<=100){
            sum+=i;//若++i在这条前面,则while条件应该是i<100
            ++i;
        }
        System.out.println(sum);
    }
}
//
//5050

2.do…while循环

对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。
do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。

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

while和do-while的区别:
while先判断后执行。dowhile是先执行后判断!
Do.….while总是保证循环体会被至少执行一次!这是他们的主要差别。

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a = 0;
        System.out.println("=======while==========");
        while(a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("======do...while=======");
        do{
            System.out.println(a);
            a++;
        }while(a<0);
        System.out.println("=====循环结束,a的值====");
        System.out.println(a);
    }
}
/*
=======while==========
======do...while=======
0
=====循环结束,a的值====
1
*/

3.for循环

虽然所有循环结构都可以用while或者do…while表示,但Java提供了另一种语句——for循环,使一些循环结构变得更加简单。
for循环语句是支持迭代的一种通用结构,是最有效、最灵活的循环结构。

public class ForDemo01 {
    public static void main(String[] args) {
        //初始化 条件判断 迭代
        for(int i=1;i<=100;i++){
            System.out.println(i);
        }
    }
}
/*
1
2
...
99
100
*/

练习1:计算0~100之间的奇数和与偶数和

public class ForDemo02 {
    public static void main(String[] args) {
        //练习1:计算0~100之间的奇数和与偶数和
        int sum1=0;
        int sum2=0;
        for(int i=1;i<=100;i++){
            if(i%2==0){
                sum2+=i;
            }else{
                sum1+=i;
            }
        }
        System.out.println("奇数和为:"+sum1);
        System.out.println("偶数和为:"+sum2);

    }
    /*
奇数和为:2500
偶数和为:2550
    */

练习2:用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个

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

        for(int i =1;i<=1000;i++){
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%(5*3)==0){//每行
                System.out.println();
            }
        }
    }
}
/*
5	10	15	
20	25	30	
...
980	985	990	
995	1000
*/

练习3:打印九九乘法表

public class Test03 {
    public static void main(String[] args) {
        //打印九九乘法表
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + "*" + j + "=" + i * j + "\t");
            }
            System.out.println();
        }
    }
}
/*
1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	
*/
//如果输出时i,j换位置
System.out.print(j + "*" + i + "=" + i * j + "\t");
/*
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
*/

4.增强for循环

用来循环数组或集合

public class Test04 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义了一个数组
        for(int i=0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("==================");
        //遍历数组的元素
        for(int x:numbers){
            System.out.println(x);
        }
    }
}
/*
10
20
30
40
50
==================
10
20
30
40
50
*/

break/continue

break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

public class Test05 {
    public static void main(String[] args) {
        int i = 0;
        for(;i<20;i++){
            System.out.print(i+"\t");
            if(i==5) break;//强制退出循环
        }
        System.out.println();
        System.out.println("=============");
        for(;i<=20;i++){
            if(i%10==0){
                System.out.println();
                continue;//只用于循环语句中,不执行本次循环剩余操作
            }
            System.out.print(i+"\t");
        }

    }
}
/*
0	1	2	3	4	5	
=============
5	6	7	8	9	
11	12	13	14	15	16	17	18	19
*/

关于goto关键字
goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子—带标签的break和continue.
“标签”是指后面跟一个冒号的标识符,例如:
对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,它们就会中断到存在标签的地方。

代码练习

打印三角形

public class PaintDemo {
    public static void main(String[] args) {
        for(int i=1;i<=5;i++){//i表示行数
            for(int j=5;j>=i;j--){
                System.out.print(" ");
            }
            for(int j=1;j<=i;j++){
                System.out.print("*");
            }
            for(int j=i-1;j>=1;j--){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
/*
     *
    ***
   *****
  *******
 *********
 
*/

如果对代码执行不理解,请学会使用debug!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值