Java——流程控制

1 用户交互Scanner

可以通过Scanner类来获取用户的输入。

基本语法:
Scanner s = new Scanner(System.in);

通过 Scanner 类的 next() 与 nextLine() 方法获取输入的字符串,在读取前一般需要使用 hasNext() 与 hasNextLine() 判断是否还有输入的数据。

next():

  1. 一定要读到有效字符后才可以结束输入。
  2. 对输入有效字符前遇到的空白,next()方法会自动将其去掉;
  3. 只有输入有效字符后才将其后面输入的空白作为分隔符或结束符
  4. next() 不能得到带有空格的字符串。

nextLine():

  1. 以 Enter 为结束符,也就是说 nextLine() 方法返回的是输入回车之前的所有字符。
  2. 可以获得空白。
package com.milk.base;
import java.util.Scanner;

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

2 顺序结构

在这里插入图片描述

3 选择结构

if 单选择结构:if ……
if 双选择结构:if …… else ……
if 多选择结构:if …… else if …… else if …… else ……
嵌套的 if 结构:if结构中嵌套if语句
switch 多选择结构
在这里插入图片描述
都要加break,防止case 穿透。

package com.milk.struc;

public class Demo1 {
    public static void main(String[] args) {
        char grade = 'C';
        //case穿透,如果不加break,会一直打印出来
        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
            case 'D':
                System.out.println("不及格");
            default:
                System.out.println("未知等级");
        }
    }
}
//输出
//及格
//不及格
//未知等级

JDK7的新特性,表达式结果可以是字符串。

package com.milk.struc;

public class Demo2 {
    public static void main(String[] args) {
         String name = "milk";
         switch (name){
             case "tea":
                 System.out.println("tea");
                 break;
             case "milk":
                 System.out.println("milk");
                 break;
             default:
                 System.out.println("Error");
        }
    }
}

4 循环结构

4.1 while 循环

package com.milk.struc;
public class Demo3 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while (i <= 100){
            sum += i;
            i++;
        }
        System.out.println(sum);

    }
}

4.2 do … while 循环

在这里插入图片描述

package com.milk.struc;

public class Demo4 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum += 1;
            i++;
        }while (i <= 100);
    }
}

4.3 for 循环

在这里插入图片描述

package com.milk.struc;

public class Demo5 {
    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);
        System.out.println("偶数和:" + evenSum);
    }
}
package com.milk.struc;

public class Demo6 {
    public static void main(String[] args) {
        //循环输出0-1000之间能被5整除的数,并且每行输出三个
        for (int i = 0; i <= 1000; i++) {
            if (i % 5 == 0){
                System.out.print(i + "\t");
            }
            if (i % (3*5) == 0){
                //两种方法都可以换行
                //println 输出完会换行 print 输出完不会换行
                System.out.println();
                //System.out.print("\n");
            }
        }
    }
}

package com.milk.struc;

public class Demo7 {
    public static void main(String[] args) {
        //打印九九乘法表
        for (int j = 0; j < 10; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i + "*" + j + "=" + (j*i) + "\t");
            }
            System.out.println();
        }
    }
}

4.4 增强 for 循环

在这里插入图片描述

package com.milk.struc;

public class Demo8 {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        //增强for循环:遍历数组中的元素
        for (int x : numbers){
            System.out.println(x);
        }
        System.out.println("=============================");
        //普通
        for (int i = 0; i < 5; i++){
            System.out.println(numbers[i]);
        }
    }
}

5 break & continue

在这里插入图片描述

package com.milk.struc;
public class Demo9 {
    public static void main(String[] args) {
        int i = 0;
        while (i < 100){
            i++;
            if (i == 30){
                //break;
                continue;
            }
            System.out.println(i);
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值