JAVA学习笔记06——流程控制

JAVA学习笔记06——流程控制

用户交互Scanner

java.util.Scanner是Java5的新特征,通过Scanner类获取用户的输入

Scanner s = new Scanner(System.in);

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

import java.util.Scanner;

public class j02scanner {
    public static void main(String[] args) {
        // 创建扫描器对象
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方式接收");
        // 判断用户有没有输入
        if(scanner.hasNext()){
            //使用nex方式接收
            String str = scanner.next();
            System.out.println("输出的内容为:" + str);
        }
        // 凡是属于IO流的类如果不关闭会一直占用资源,要及时关闭
        scanner.close();
    }
}

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

next():

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

nextLine():

  1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符
  2. 可以获得空白
    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("不是整数");
        }
    }

顺序结构

从上到下执行

选择结构

if单选择结构

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

案例

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容");
        String s = scanner.nextLine();
        if(s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
    }

if双选择结构

if(布尔表达式){
    //为true
}else{
    //为false
}

案例

// if双选择结构
    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();
    }

if多选择结构

if(布尔表达式1){
    //
}else if(布尔表达式2){
    //
}else{
    //
}

案例

    // if 多选择结构
        public static void main(String[] args) {
        // 考试分数大于60就及格,小于60不及格
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if(score == 100){
            System.out.println("满分");
        }else if(60 < score && score <100){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }
        scanner.close();
    }

switch 多选择结构

switch case语句判断一个变量与一个系列值中的某个值是否相等,每个值称为一个分支

switch语句中的变量类型可以是:

  • byte、short、int或者char
  • switch支持字符串string类型
  • case标签必须为字符串常量或字面量
switch(expression){
    case value:
        //语句
        break;//
    case value:
        break;
    default:
        //语句
}

案例

    public static void main(String[] args) {
        char grade = 'C';
        switch(grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("一般");
        }
    }

循环结构

while循环

while(布尔表达式){
    //循环结构
}

案例循环输出1~100

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

do while 循环

循环至少会执行一次

do{
    //
}while(布尔);

while与do while区别

do while至少会执行一次先执行后判断

for循环

循环结构支持迭代的一种通用结构

for循环执行的次数是在执行前就确定的

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

案例

    //for循环
    public static void main(String[] args) {
        int a = 1;
        while(a <= 100){
            System.out.println(a);
            a += 2;
        }
        System.out.println("循环结束");
        for(int i = 1;i <=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束");
    }

0到100的奇数和偶数和

public static void main(String[] args) {
        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);
    }

增强for循环

for(声明语句:表达式)
{
    
}

案例

    // 增强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 x:numbers){
            System.out.println(x);
        }
    }

声明语句声明新的变量

break continue

break强行退出循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值