Java流程控制

Scanner对象

  1. Java提供了一个工具类,可以获取用户的输入。java.util.Scanner是Java5的新特征。
  • 基本语法:Scanner scanner = new Scanner(System.in);
  • 使用后关闭:scanner.close();
  1. 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
  • //使用next方式接收

  • 基本语法:if(scanner.hasNext()){

  • String str = scanner.next();

  • }

  • //使用nextLine方式接受

  • 基本语法:if(scanner.hasNextLine()){

  • String str = scanner.nextLine();

  • }

  1. next():
  • 一定要读取到有效字符后才可以结束输入
  • 对输入有效字符之前遇到的空白,next()方法会自动将其去掉
  • 只有输入有效字符后才可以将其后面输入的空白作为分隔符或者结束符
  • 输入有效字符后可以以Enter作为结束符
  • next()不能得到带有空格的字符串
  1. nextLine():
  • 以Enter为结束符,也就是说nextLine方法返回的是输入回车之前的所有字符
  • 可以获得空白
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);
        }

        scanner.close();
    }
}
public class Demo2 {
        public static void main(String[] args) {
            //创建一个扫描器对象,用于接收键盘数据
            Scanner scanner = new Scanner(System.in);
            System.out.println("使用nextLine方式接受:");
            //从键盘接受用户输入的字符串
            if (scanner.hasNextLine()){
                //使用nextLine方式接收
                String str = scanner.nextLine();
                System.out.println("输出的内容为:"+str);
            }

            scanner.close();
        }
}
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("请输入整数:");
        //使用nextInt方式接收
        if (scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println("整数数据:"+i);
        }else{
            System.out.println("输入的不是整数数据");
        }
        System.out.println("请输入小数:");
        //使用nextFloat方式接收
        if (scanner.hasNextFloat()){     //将整型数据转化为浮点数然后判断,如果是浮点数则直接判断
            f = scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }else{
            System.out.println("输入的不是小数数据");
        }

        scanner.close();
    }
}
public class Demo4 {
    public static void main(String[] args) {
        //输入多个数字,并求其总和与平均数,每输入一个数字用回车(不是空格)确认,通过输入非数字来结束输入并输出执行结果
        Scanner scanner = new Scanner(System.in);
        //和
        double sum = 0.0;
        //计算输入了多少个数字
        int number = 0;
        //通过while循环判断是否还有输入,并在里面对每一次进行求和和统计
        while (scanner.hasNextDouble()){  //将整型数据转化为浮点数然后判断,如果是浮点数则直接判断
            double x = scanner.nextDouble();
            number++;
            sum += x;
            System.out.println("你输入了第"+number+"个数据,然后当前结果sum是"+sum);
        }
        System.out.println(number+"个数字的和为"+sum);
        System.out.println(number+"个数字的平均值为"+(sum/number));


        scanner.close();
    }
}

顺序结构

语句与语句之前,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。

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

选择结构

If单选择结构

public class IfDemo01 {
    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双选择结构

public class IfDemo02 {
    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 class IfDemo03 {
    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结构

使用嵌套的if...else语句是合法的。可以在另一个 if 或者 else if 语句中使用 if 或者 else if 语句。

public class IfDemo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if (score>60&&score<=100){
            if(score>=90){
                System.out.println("成绩优秀");
            }
        }
        scanner.close();
    }
}

switch多选择结构

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

  • swich 语句中的变量类型可以是:byte、short、int、char、String

  • case标签必须为字符串常量或者字面量

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade = 'C';

        switch (grade){

            case 'A':
                System.out.println("A级");
                break;
            case 'B':
                System.out.println("B级");
                break;
            case 'C':
                System.out.println("C级");
                break;
            case 'D':
                System.out.println("D级");
                break;
            case 'E':
                System.out.println("E级");
                break;
            default:
                System.out.println("等级未知");
                
        }
    }
}
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "张三";
        switch (name){
            case "张三":
                System.out.println("张三");
                break;
            case "李四":
                System.out.println("李四");
                break;
            default:
                System.out.println("未知姓名");
        }
    }
}

//对应的反汇编文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.huangshen.struct;

public class SwitchDemo02 {
    public SwitchDemo02() {
    }

    public static void main(String[] args) {
        String name = "张三";
        byte var3 = -1;
        switch(name.hashCode()) {
        case 774889:
            if (name.equals("张三")) {
                var3 = 0;
            }
            break;
        case 842061:
            if (name.equals("李四")) {
                var3 = 1;
            }
        }

        switch(var3) {
        case 0:
            System.out.println("张三");
            break;
        case 1:
            System.out.println("李四");
            break;
        default:
            System.out.println("未知姓名");
        }

    }
}

File->Project Structure->字节码文件

Open In->Explorer->类文件

将字节码文件拖入IDEA对应的类文件夹->字节码文件的反汇编文件

循环结构

while循环结构

  • 只要布尔表达式为true,循环就会一直执行下去
  • 大多数情况是会让循环停止下来,需要一个让表达式失效的方式来结束循环
  • 少部分情况需要循环一直执行,比如服务器的请求监听等
  • 循环条件一直为true就会造成无限循环,正常的业务编程应该尽量避免死循环,会影响程序性能或者造成程序卡死崩溃
//1+2+3+...+100
public class WhileDome {
    public static void main(String[] args) {
        //1+2+3+...+100
        int i = 0;
        int sum = 0;
        while(i<100){
            i++;
            sum += i;
        }
        System.out.println(sum);
    }
}

do-while循环

  • do-while 循环的特点不同于while循环,是先做后判断,所以至少循环体至少会执行一次
public class DoWhileDemo {
    public static void main(String[] args) {
        //1+2+3+...+100
        int i = 0;
        int sum = 0;
        do {
            i++;
            sum += i;
        }while(i<100);
        System.out.println(sum);
    }
}

for循环

  • for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构
  • for循环执行的次数是在执行前就确定的,语法格式如下
  • for(初始化;布尔表达式;更新){
  • //代码语句
  • }
//计算0到100之间奇数和偶数的和
public class ForDemo01 {
    public static void main(String[] args) {
        int oddsum = 0;
        int evensum = 0;
        for (int i = 0; i <=100; i++) {
            if (i%2==0){
                evensum += i;
            }else {
                oddsum += i;
            }
        }
        System.out.println("偶数和为"+evensum);
        System.out.println("奇数和为"+oddsum);
    }
}
//输出1-1000内能被5整除的数,并且每行输出三个
public class ForDemo02 {
    public static void main(String[] args) {
        for (int i = 1; i <=1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");  //print输出完不会换行
                if (i % 15 == 0) {
                    System.out.println();   //println输出完会换行
                }
            }
        }
    }
}
//打印9*9乘法表
public class ForDemo03 {
    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+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    }
}

增强for循环

  • Java5引入了一种主要用于数组或集合的增强型for循环
  • Java 增强for循环语法格式如下:
  • for(声明语句:表达式){
  • //代码句子
  • }
  • 声明语句:声明新的局部变量。该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值于此时数组元素的值相等。
  • 表达式:表达式时要访问的数组名,或者时返回值为数组的方法。
public class ForDemo04 {
    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

  • break在任何循环语句的主体部分,均可用break控制循环的流程
  • break用于强行退出循环,不执行循环中剩余的语句
  • break语句也在switch语句中使用
public class BreakDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==30){
                System.out.println();
                break;
            }
        }
    }
}

continue

  • continue语句在循环语句体中,用于中段某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次事都执行循环的判定
public class ContinueDemo01 {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            if (i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i+"\t");
        }

    }
}

label

  • goto仍是Java的一个关键字,但并未在语言中得到正式使用
  • Java没有goto,然而在break和continue上能看出goto的影子--带标签的break和continue
  • "标签"是指后面跟一个冒号的标识符,例如 label:
  • 对Java来说为一种到标签的地方是在循环语句前,而在循环之前设置标签的唯一理由是
  • 希望在其中嵌套另一个循环,由于break和continue关键字通常只中断当前循环,但若随同标签使用,他们就会中断到存在标签的地方
public class LabelDemo01 {
    public static void main(String[] args) {
        //打印101-150之前的所有质数
        outer:for (int i = 101; i <= 150; i++) {
            for(int j = 2;j<i/2;j++){
                if(i%j==0){
                    continue outer;
                }
            }
            System.out.print(i+"\t");
        }
    }
}

小作业

  • 打印三角形
  • 思想:
  • 3个循环,第一个循环打印空格,第二个循环打印 * 号,第三个循环打印第二个循环个数-1的 * 号
public class TestDemo01 {
    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();
        }
    }
}
posted @ 2024-03-02 20:36  qing集  阅读(4)  评论(0编辑  收藏  举报