java3:流程控制

Java流程控制

Scanner对象

注意:Scanner依赖于 java.util.Scanner 包;使用Scanner对象需导入包名,可通过idea自动添加

获取字符串

  • 通过Scanner类的 next()nextLine() 方法获取输入的字符串
  • 在读取前一般需要使用 hasNext()hasNextLine() 判断是否还有输入数据
1. 使用 next() 方法获取数据
  • 不能获取 纯空格 字符
import java.util.Scanner;  // 使用Scanner需导入此包

public class Test_scanner {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in); // 创建输入扫描器
        System.out.println("----使用next方法接收----");
        System.out.print("请输入数据:");
        if (scanner.hasNext()) {
            String str = scanner.next(); // 获取到空格,结束获取
            System.out.println("输出内容为:" + str);
        }
        scanner.close(); // 关闭扫描器,节省资源
    }
}

输出内容是不是觉得很奇怪???

  • 因为当 next() 方法读取到 ”空格“ 时自动结束获取数据(空格为结束符)
----使用next方法接收----
请输入数据:hello world
输出内容为:hello
2. 使用 nextLine() 方法获取数据
  • 所以需要获取带空格的数据时,应该用 nextLine() 方法
  • 可以获取 纯空格 字符
  • 使用场景最多
import java.util.Scanner;

public class Teat_scanner_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("----使用nextLine方法接收数据----");
        System.out.print("请输入数据:");
        if (scanner.hasNext()) {
            String str = scanner.nextLine(); // 获取到回车,结束获取
            System.out.println("输出内容为:" + str);
        }
        scanner.close();
    }
}
----使用nextLine方法接收数据----
请输入数据:hello world
输出内容为:hello world
3. 简洁写法
  • 由于开发时会涉及到大量的数据,所以在需要对输入数据进行判断 if (scanner.hasNext()) {xxxx}

去除判断也能实现同样的效果

import java.util.Scanner;

public class Teat_scanner_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("----使用nextLine方法接收数据----");
        System.out.print("请输入数据:");
        String str = scanner.nextLine(); // 获取到回车,结束获取
        System.out.println("输出内容为:" + str);    
        scanner.close();
    }
}
----使用nextLine方法接收数据----
请输入数据:123 123 123
输出内容为:123 123 123
4. 不同类型的数据获取
  • scanner.hasNextInt() scanner.hasNextFloat() 判断数据为 整数 / 小数
  • scanner.hasNextDouble() 同理
import java.util.Scanner;

public class Scanner_int_float {
    public static void main(String[] args) {
        // 数据初始化
        int i = 0;
        float f = 0.0f;

        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入整数:");
        if (scanner.hasNextInt()) {
            i = scanner.nextInt();
            System.out.println("该整数为:" + i);
        } else {
            System.out.println("输入非整数!");
        }

        System.out.print("请输入小数:");
        if (scanner.hasNextFloat()) {
            f = scanner.nextFloat();
            System.out.println("该小数为:" + f);
        } else {
            System.out.println("输入非小数!");
        }
        scanner.close();
    }
}
请输入整数:11
该整数为:11
请输入小数:1.1
该小数为:1.1
请输入整数:1.1
输入非整数!
请输入小数:该小数为:1.1
5. 循环输入数据
import java.util.Scanner;

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

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据:");
        while (scanner.hasNextDouble()) {
            double x = scanner.nextDouble();
            i += 1;
            sum += x;
        }
        System.out.println("i = " + i);
        System.out.println("sum = " + sum);
    }
}
  • 可通过空格区分数据
请输入数据:
1 1
a
i = 2
sum = 2.0
  • 也可通过回车区分数据
请输入数据:
1
2
3
q
i = 3
sum = 6.0

顺序结构

就是从上到下按顺序执行,几乎是所有语言最常用的结构,这里就不细讲

选择结构

1. if单选择结构

// 注意:这种是错误的判断语句,“==”表示的是内存地址一致才输出true
if(str=="hello"){
	System.out.println("hi");
}
import java.util.Scanner;

public class If_Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入:");
        String str = scanner.nextLine();
        if(str.equals("hello")){
            System.out.println("hi");
        }
        scanner.close();
    }
}
请输入:hello
hi

2. if多选择结构

import java.util.Scanner;

public class If_Test {
   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("请输入分数:");
       int score = scanner.nextInt();
       /*
       从小到大判断,可以简便代码,任何一个条件为true,即输出结束判断
        */
       if (score<60){
           System.out.println("D");
       } else if (score<80) {
           System.out.println("C");
       } else if (score<90) {
           System.out.println("B");
       } else {
           System.out.println("A");
       }
       scanner.close();
   }
}
请输入分数:90
A

3.switch多选择结构

1. 示例1
public class Switch_Test {
    public static void main(String[] args) {
        char grade = 'A';
        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;
            default:
                System.out.println("输入有误");
        }
    }
}
优秀

case 具有穿透性(若不添加break停止运行,则程序会一直往下输出),看下以下示例:

2. 示例2
public class Switch_Test {
    public static void main(String[] args) {
        char grade = 'A';
        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;
            default:
                System.out.println("输入有误");
        }
    }
}
优秀
良好
及格
不及格
输入有误
3. 示例3(重点)

从java se 7开始,switch开始支持字符串String类型

public class Swicht_Test {
    public static void main(String[] args) {
        String name = "Joy Won";
        switch (name){
            case "Joy Won":
                System.out.println("帅哥");
                break;
            case "joy won":
                System.out.println("美女");
                break;
            default:
                System.out.println("未知性别");
        }
    }
}
帅哥
  • 思考:字符串的匹配是什么原理???
    • 字符串的本质还是数字
  • 我们可以通过反编译查看字符串对应的数值(hashcode)
  • .java文件 通过编译变成 .class文件(给计算机看的文件),我们可以通过idea实现 反编译,进而查看String对应的数值,以下是教程:
  1. 打开项目结构(File — Project Structure),快捷键:ctrl+alt+shift+s ,查看编译文件保存的目录

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  1. 想方设法把这 .class 文件复制到idea中,但是意外发生了,我们可以通过拖动的方式将文件导入

在这里插入图片描述

  1. 查看到case中的字符串其实是一串数字(hashcode),通过插件查看

在这里插入图片描述

  1. 插件下载使用方式

    • 下载:File–Setting–Plugins–搜索栏(下载 bytecode viewer)

      在这里插入图片描述

    • 使用:view–show bytecode(当前页面需要保留在.class文件页面)

      在这里插入图片描述

4.循环结构(while,do…while,for)

1. while结构
  • while实现1累加到100
public class While_Test {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        while (i<=100){
            sum += i;
            i++;
        }
        System.out.println("和为:"+sum);
    }
}
和为:5050
2. do…while
  • 对于while而言,不满足条件即不能进入循环,但有时我们需要即使不满足条件也要至少执行一次
public class While_Test {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        do {
            sum += i;
            i++;
        }while (i<=100);
        System.out.println("和为:"+sum);
    }
}
和为:5050
3. for
  • 所有的while循环都能用for循环代替,for循环更加高效

例1:

public class For_Test {
   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);
   }
}
奇数和:2500
偶数和:2550

例2:输出0-100被5整除的数,每三个数据一行

public class For_Test {
    public static void main(String[] args) {
        int flag = 0;
        for (int i = 0; i <= 100; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");
                flag++;
                if (flag % 3 == 0) {
                    System.out.println();
                }
            }
        }
    }
}
0	5	10	
15	20	25	
30	35	40	
45	50	55	
60	65	70	
75	80	85	
90	95	100	

例3:打印九九乘法表

public class For_Test {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + i * j + "\t");
            }
            System.out.println();
        }
    }
}
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	

例4:增强for循环

public class For_Test {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};
        /*
        两种循环输出结果相同,都是循环取出数组numbers中的值
        */
        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
4. break 和 continue
  • break:结束循环
  • continue:结束这一次循环,继续下一次循环
public class For_Test {
    public static void main(String[] args) {
        int i = 0;
        /*
        被3整除的数不输出,其他分行输出
        */
        while (i < 10) {
            i++;
            if (i % 3 == 0) {
                System.out.println();
                continue;
            }
            System.out.print(i + "\t");
        }
    }
}
1	2	
4	5	
7	8	
10

5. 练习

输出一个三角形

package com.joywon.scanner;

public class Test {
    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();
        }
    }
}
     *
    ***
   *****
  *******
 *********
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值