Java流程控制笔记【自用】

本文章参考B站up主遇见狂神说Java基础课程中的Java流程控制部分教学视频。笔记只做分享用途。如果有错误欢迎评论区讨论~

用户交互Scanner

  • Java提供了Scanner类来获取用户的输入
java.util.Scanner;

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

示例1

package com.kuang.Scanner;

import java.util.Scanner;

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

示例2

使用nextLine()

package com.kuang.Scanner;

import java.util.Scanner;

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

        System.out.println("使用nextLine方式接受:");

        if(scanner.hasNextLine()){
            String str  = scanner.nextLine();
            System.out.println("用户输入内容为:"+str);

        }
        scanner.close();
    }
}

上面代码输出为:

使用nextLine方式接受:
hello world
用户输入内容为:hello world
  • 总结:
    • next():
      1. 一定要读取到有效字符后才可以结束输入
      2. 对输入有效字符之前遇到的空白,next() 方法会将其去掉
      3. 只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
      4. next() 不能得到带有空格的字符串。
    • nextLine():
      1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
      2. 可以获得空白.

示例3

scanner进阶用法

package com.kuang.Scanner;

import java.util.Scanner;

public class demo3 {
    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("输入的不是整数!");
        }
    }
}

上面代码的输出为:

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

示例4

package com.kuang.Scanner;

import java.util.Scanner;

public class demo4 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);  //接受用户输入

        double sum = 0;
        int m =0;

        while (scanner.hasNextDouble()){  //while语句
            double x = scanner.nextDouble();

            m = m + 1;
            sum = sum + x;
        }

        System.out.println(m+"个数的和为:"+sum);
        System.out.println(m+"个数的平均值是"+(sum/m));
        
        scanner.close();
    }
}

上面代码输出为:

10
10
20
20
;
4个数的和为:60.0
4个数的平均值是15.0

顺序结构

  • 顺序结构
package com.kuang.struct;

public class SequenceDemo {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
    }
}

选择结构

if 单选择结构

if(布尔表达式){
    //如果布尔表达式为true将执行的语句
}

示例1

package com.kuang.struct;

import java.util.Scanner;

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

    }
}

if双选择结构

if(布尔表达式){
    //如果布尔表达式的值为true
}else{
    //如果布尔表达式的值为false
}

示例2

package com.kuang.struct;

import java.util.Scanner;

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

        System.out.println("请输入成绩:");
        int s  = scanner.nextInt();
        //考试分数大于60分就是及格,小于60分就不及格
        if (s > 60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }

        System.out.println("End");
        scanner.close();

    }
}

if 多选择结构

if(布尔表达式1){
    //如果布尔表达式1的值为true执行代码
}else if(布尔表达式2){
    //如果布尔表达式2的值为true执行代码
}else if(布尔表达式3){
    //如果布尔表达式3的值为true执行代码
}else {
    //如果布尔表达式的值都不为true执行代码
}

示例3

package com.kuang.struct;

import java.util.Scanner;


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

        System.out.println("请输入成绩:");
        int s  = scanner.nextInt();
        //考试分数大于60分就是及格,小于60分就不及格
        if (s == 100){
            System.out.println("恭喜满分");
        }else if(s<100 && s>=90){
            System.out.println("A级");
        }else if(s<90 && s>=80){
            System.out.println("B级");
        }else if(s<80 && s>=70){
            System.out.println("C级");
        }else if(s<70 && s>=60){
            System.out.println("D级");
        }else if(s<60 && s>=0){
            System.out.println("不及格");
        }else{
            System.out.println("成绩不合法");
        }
        System.out.println("End");
        scanner.close();
    }
}

switch多选择结构

  • switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支
switch(expression){
    case value :
        //语句
        break; //可选
     case value :
        //语句
        break; //可选
    //你可以有任意数量的case语句
    default: //可选
        //语句     
}

示例4

package com.kuang.struct;

public class switchDemo1 {
    public static void main(String[] args) {
        //
        char grade = 'F';

        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;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");
        }
    }
}

循环结构

while 循环

while(布尔表达式){
    //循环内容
}

示例1

package com.kuang.struct;

public class whileDemo1 {
    public static void main(String[] args) {
        int i = 0;

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

示例2

package com.kuang.struct;

public class whileDemo2 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

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

do while 循环

do {
    //循环内容
}while(布尔表达式);

示例3

package com.kuang.struct;

public class doWhileDemo2 {
    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);
    }
}

for 循环

for(循环;布尔表达式;更新){
    //代码语句
}

示例4

package com.kuang.struct;

public class forDemo1 {
    public static void main(String[] args) {
        int a = 1;

        while (a<=100){
            System.out.println(a);
            a += 2;
        }
        System.out.println("while循环结束");

        for(int i=1;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束");
        //100.for 回车,快速生成循环语句
    }
}

示例5

package com.kuang.struct;

public class forDemo3 {
    public static void main(String[] args) {
        int oddSum = 0;
        int evenSum = 0;
        for (int i = 0; i < 100; i++) {
            if (i%2 == 1){
                oddSum += i;
            }else{
                evenSum += i;
            }
        }
        System.out.println(oddSum);
        System.out.println(evenSum);
    }
}

示例6

package com.kuang.struct;

public class forDemo2 {
    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();
            }
        }
        //println 输出会换行
        //print 输出不会换行
    }
}

示例7

package com.kuang.struct;

public class forDemo4 {
    public static void main(String[] args) {
        for (int j = 1; j < 10; j++) {
            for (int i = 1; i < 10; i++) {
                if(i<j){
                    System.out.print(i+"*"+j+"="+(i*j));
                    System.out.print("\t");
                }else if(i==j){
                    System.out.println(i+"*"+j+"="+(i*j));
                }
            }
        }
    }
}

上述代码输出为:

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

break & continue

示例1

package com.kuang.struct;

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

示例2

package com.kuang.struct;

public class continueDemo {
    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);
        }
    }
}
  • 21
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值