5.Java -流程控制

本文介绍了Scanner对象在Java中的使用,包括hasNext、hasNextLine、nextInt和nextFloat,展示了如何处理不同类型的数据输入。此外,详细讲解了顺序结构、选择结构(if-else、if-else if-else和switch)、循环结构(while, do-while, for)以及break和continue的运用,通过实例演示了各种编程控制结构的实际操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 、用户交互Scanner对象

1. hasNext

package com.fj.scanner;

import java.util.Scanner;

//  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();
    }
}

hasNext 返回结果

使用next 方式接收:

hello world
输出的内容为:hello

Process finished with exit code 0

2. hasNextLine

package com.fj.scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收:");
        //判断是否还有输入,以 enter结束
        if(scanner.hasNextLine()){
            //用next方式接收
            String str = scanner.nextLine();//程序会等待用户输入完毕
            System.out.println("输出内容为:"+str);
        }
        //凡是属于io的类,如果不关闭会一直占用资源,用完就关掉
        scanner. close();
    }
}

hasNextLine返回结果

使用next方式接收:
hello world
输出内容为:hello world

Process finished with exit code 0

next 和 nextLine的区别:
在这里插入图片描述
去掉if判断:

package com.fj.scanner;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据:");
        //用next方式接收
        String str = scanner.nextLine();//程序会等待用户输入完毕
        System.out.println("输出内容为:" + str);
        //凡是属于io的类,如果不关闭会一直占用资源,用完就关掉
        scanner.close();
    }
}

结果:

请输入数据:
欢迎学习java  欢迎大家学习
输出内容为:欢迎学习java  欢迎大家学习

Process finished with exit code 0

3. hasNextInt 和 hasNextFloat

package com.fj.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();
    }
}

正确结果返回:

请输入整数:

10
整数数据:10
请输入小数:
0.01
小数数据:0.01

错误返回:

请输入整数:
11来来来
输入的不是整数数据
请输入小数:
输入的不是小数数据

其他返回,整数输入小数,整数报错,小错返回正确:

请输入整数:
10.1
输入的不是整数数据
请输入小数:
小数数据:10.1

4. 数据相加练习题,输入任性字符终止

package com.fj.scanner;

import java.util.Scanner;

//
public class Demo05 {
    public static void main(String[] args) {
        //我们可以输入多个数字,并求总和与平均数,每输入一个数字用回车确认,
        //通过输入非数字来结束输入并输出执行结果
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数字:");
        //和
        double sum = 0;
        //计算输入了多少个数字
        int m = 0;
        //通过循环判断是否还有输入,并在里面每一次进行求和和统计
        while (scanner.hasNextDouble()) {
            double x = scanner.nextDouble();
            //计算输入了多少数字
            m = m + 1;//m++,累加次数,每次加1
            sum = sum + x;//累加数据,每次加输入的数字
            System.out.println("你输入了第"+m+"个数据,当前结果是sum="+sum);
        }
        System.out.println(m+"个数的和为:"+sum);
        System.out.println(m+"个数平均值是:"+(sum/m));
        scanner.close();
    }
}

结果:

请输入数字:
10
你输入了第1个数据,当前结果是sum=10.0
20
你输入了第2个数据,当前结果是sum=30.0
30
你输入了第3个数据,当前结果是sum=60.0
50
你输入了第4个数据,当前结果是sum=110.0
x
4个数的和为:110.0
4个数平均值是:27.5

Process finished with exit code 0

二 、顺序结构

按从上到下,一步步执行的结构,叫顺序结构

  1. 实例
package com.fj.struct;

public class Demo01 {
    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

Process finished with exit code 0

三 、选择结构

1. if 单选结构

判断一个东西是否可行,用if单选结构

package com.fj.struct;

import java.util.Scanner;

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

    }
}

执行结果:

请输入内容:
Hello
Hello
End

Process finished with exit code 0

2. if双选结构

如果正确走一个分支,反之走另一个分支

package com.fj.struct;

import java.util.Scanner;

//if双选结构
public class IfDemo02 {
    public static void main(String[] args) {
        //考试分数大于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
不及格

3. if多选结构

多种条件判断

package com.fj.struct;

import java.util.Scanner;

// if多选结构
public class IfDemo03 {
    public static void main(String[] args) {
        //考试分数大于60就是及格,反正就不及格
        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();
    }
}

结果

请输入考试分数:
60
DProcess finished with exit code 0

4. 嵌套的if结构

多层if嵌套

if(){
   if(){
   }
}

5. switch多选结构

switch:用于具体字符,if用于区间判断

package com.fj.struct;

public class SwitchDemo01 {
    //switch
    public static void main(String[] args) {
        char grade = 'C';
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;//可选,case穿透
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("再接再厉");
                break;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("位置等级");
                break;
        }
    }
}

结果

及格

Process finished with exit code 0

switch: 字符串判断

package com.fj.struct;
//switch 字符串比较
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "fj";
        switch (name){
            case "fj":
                System.out.println("fj");
                break;
            case "订单":
                System.out.println("订单");
                break;
            default:
                System.out.println("弄啥嘞");
                break;
        }

    }
}

结果:

fj

Process finished with exit code 0

扩展知识反编译:查看idea反编译文件

  1. 查看反编译路径file->project structure

在这里插入图片描述

  1. 查看编译路径
    在这里插入图片描述

  2. 查看当前路径下的反编译文件
    在这里插入图片描述

  3. 通过文件夹的方式复制编译文件到当前项目文件夹下,不可直接ieda复制,会报错

  4. idea自动加载文件夹新添加的文件,case里面的判断用的都是编码方式判断
    在这里插入图片描述

四 、循环结构

1. while 循环

只要为true,会一直执行,大多数需要让循环停止下来,让循环失效,如果一直循环会形成死循环

  1. while循环实例
package com.fj.struct;
//while 循环
public class WhileDemo01 {
    public static void main(String[] args) {
        // 输出1-100
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}

while循环结果:

94
95
96
97
98
99
100

Process finished with exit code 0
  1. 死循环代码:
package com.fj.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环,程序崩溃
        while (true){
            //等待客户端链接,定时检查,。。。。

        }
    }
}

  1. 计算1+2+3…+100
package com.fj.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+4....+100
        int i = 0;
        int sum = 0;
        while (i <= 100){
            sum = sum + i;
            i ++;

        }
        System.out.println(sum);
    }
}

结果:

5050

Process finished with exit code 0

2. do…while 循环

do…while :至少执行一次

  1. 实例:
package com.fj.struct;
//do while
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        //至少执行一次
        do{
            sum = sum + i;
            i ++;
        }while (i <= 100);
        System.out.println(sum);
    }
}

  1. while和do…while区别实例
package com.fj.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);


    }
}

结果:

======================
0

Process finished with exit code 0

3. for 循环

  1. for 循环说明

for(初始化;布尔表达式;更新){
}

  1. 实例:
package com.fj.struct;

//for
public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1;//初始化条件
        while (a<=100){//条件判断
            System.out.println(a);//循环体
            a+=50;//迭代
        }
        System.out.println("while循环结束");
        //初始化;条件判断;迭代
        for(int i =1;i<=100;i++){
            System.out.println(i);//循环体
        }
        System.out.println("for循环结束");

    }
}

结果:

1
51
while循环结束
1
2
3
...//太长省略了
100
for循环结束
  1. 练习1:用for循环计算0-100的奇数和偶数的和
package com.fj.struct;

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

结果:

奇数和为:2500.0
偶数和为:2550.0

Process finished with exit code 0
  1. 练习2:用while或for循环输出1-1000之间能被五整除的数,并且每行输出三个
package com.fj.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //练习2:用while或for循环输出1-1000之间能被五整除的数,并且每行输出三个
        //println 输出完就换行
        //print 输出完不换行
        for (int i = 1; i <=1000;i++){
            if (i % 5 == 0){
                System.out.print(i+"\t");//是print 不是println
            }
            if (i%(5*3)==0){ //每行输出三个
                System.out.println();
            }
        }
    }
}


结果:

... //太多省略了
905	910	915	
920	925	930	
935	940	945	
950	955	960	
965	970	975	
980	985	990	
995	1000	
Process finished with exit code 0

5.练习3: 九九乘法表

package com.fj.struct;
//打印99乘法表
/*
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
 */
public class ForDemo04 {
    public static void main(String[] args) {
        //1. 打印第一列,
        //2. 把固定的1用循环包起来
        //3.去掉重复项 i<=j
        //4.调整样式
        for (int j =1; j<=9; j++){
            for(int i =1;i<=j;i++){
                System.out.print(j+"*"+i+"="+(j*i)+"\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	

Process finished with exit code 0
  1. 增强for循环
package com.fj.struct;

//增强 for循环
public class ForDemo05 {
    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

Process finished with exit code 0

五 、break & continue

break:终止循环
continue:跳出当前循环,进入下一次循环

1. brak:终止循环

package com.fj.struct;

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


结果:到30终止循环

....//省略
26
27
28
29
123

Process finished with exit code 0

2. continue:跳出当前循环,进入下一个循环

package com.fj.struct;

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

结果:跳出i=3的当前循环,进入下一次i=4的循环

1
2
4
5

Process finished with exit code 0

3. goto 标签,只要了解

package com.fj.struct;
//goto 标签
public class LabelDemo01 {
    public static void main(String[] args) {
        // 打印101-150之间的所有质数
        //质数:是指大于1的自然数中,除了1和他本身以外不能被其他数整除的数
        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+"");

        }
    }
}

结果:

101
103
107
109
113
127
131
137
139
149

Process finished with exit code 0

六 、练习

1. 打印三角形

package com.fj.struct;
//练习题1;打印三角形
public class TestDemo01 {
    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();
        }

    }
}

结果:

     *
    ***
   *****
  *******
 *********

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值