Java流程控制

Java流程控制

用户交互Scanner

  • 在这里插入图片描述

  • next()和nextLine()的区别

在这里插入图片描述

next()

//创建一个扫描器对象,用于接受键盘数据
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();

结果,如下图

在这里插入图片描述

nextLine()

//创建一个扫描器对象,用于接受键盘数据
Scanner scanner = new Scanner(System.in);

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

//判断用户有没有输入字符串
if (scanner.hasNextLine()){
    //使用nextLine方式接收
    String str = scanner.nextLine();
    System.out.println("输出的内容为:"+str);
}

//凡是属于IO流的类如果不关闭会一直占用资源,用完就关掉
scanner.close();

结果,如下图

在这里插入图片描述

Scanner进阶使用

判断输入的数值类型是否正确

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

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


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

    if (scanner.hasNextInt()){
        i = scanner.nextInt();
        System.out.println("整数数据:"+i);
    }else {
        System.out.println("输入的不是整数数据!");
    }

    //float
    System.out.println("请输入小数:");

    if (scanner.hasNextFloat()){
        f = scanner.nextFloat();
        System.out.println("小数数据:"+f);
    }else {
        System.out.println("输入的不是小数数据!");
    }

    scanner.close();
}

结果,如下图

在这里插入图片描述

求输入值的和与平均值

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.nextDouble();
        m++;
        sum+=x;
    }
    System.out.println(m+"个数的和为:"+sum);
    System.out.println(m+"个数的平均值为:"+(sum/m));

    scanner.close();
}

结果,如下图

在这里插入图片描述

顺序结构

自上而下

  • 在这里插入图片描述

选择结构

if单选结构

在这里插入图片描述

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);
    }else {
        System.out.println("end");
    }

    scanner.close();
}

结果

在这里插入图片描述

if双选择结构

在这里插入图片描述

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

结果

在这里插入图片描述

if多选择结构

在这里插入图片描述

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

结果

在这里插入图片描述

嵌套的if结构

在这里插入图片描述

switch多选择结构

在这里插入图片描述

public static void main(String[] args) {

        //case穿透 需要加break
        //switch 具体匹配一个值
        char grade = 'C';

        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循环

在这里插入图片描述

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

do…while循环

在这里插入图片描述


for循环

在这里插入图片描述

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);//2500
    System.out.println("偶数和为:"+evenSum);//2550
}
public static void main(String[] args) {
    //99乘法表
    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();
    }

}

增强for循环

在这里插入图片描述

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 number : numbers) {
        System.out.println(number);
    }
//上下结果相同
}

break和continue

在这里插入图片描述

public static void main(String[] args) {
    int i = 0;
    int j = 0;
    while (i<100){
        i++;
        System.out.println(i);
        if (i==30) break;//直接跳出while循环
    }

    System.out.println("========================");

    while (j<100){
        j++;
        if (j==30){
            continue;//返回到while,不再往下执行
        }
        System.out.println(j);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值