流程控制语句

用户交互Scanner

之前的学习中并没有出现程序和人交互的案例,Java中为我们提供了交互的工具,Scanner类就是实现交互的一种类。
Scanner类是用来接受用户输入的数据的。常与next(),nextLine()配合使用
next()特点:
1.读取到有效字符后才会结束输入
2.不能得到有空格的字符串
代码演示

public class Scanner01 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("使用next方式接收:");
        if (sc.hasNext()){
            String str=sc.next();
            System.out.println("输出内容为:"+str);
        }
    sc.close();//凡是IO流用完就要关掉,不然会占用资源
    }
}

nextLine()特点:
1.以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符。
2.可以获得空白
代码演示

import java.util.Scanner;
public class Scanner02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("使用nextline方式接收:");
        if (sc.hasNextLine()){
            String str=sc.nextLine();
            System.out.println("输出内容为:"+str);
        }
        sc.close();//凡是IO流用完就要关掉,不然会占用资源
    }
}

运行结果:
请输入图片描述

Scanner进阶使用

如果要控制输入的必须是整数应该怎么做?
Scanner有很多配套的方法,为我们提供了解决方案。我们可以控制输入数据的类型为整数,小数等。
请输入图片描述
代码演示:

import java.util.Scanner;

public class Scanner03 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        if (scanner.hasNextInt()){
            int x=scanner.nextInt();
            System.out.println("输出结果为:"+x);
        }else
            System.out.println("Error,输入的不是整数");

    }
}

运行结果:
请输入图片描述
请输入图片描述

利用Scanner做一个小算法题,要求输入多个数字,求出输入数字的总和及平均值。
代码演示:

import java.util.Scanner;

public class Scanner04 {
    public static void main(String[] args) {
        int sum=0;
        int total=0;
        Scanner scanner=new Scanner(System.in);
    while (scanner.hasNextInt()){
        int x=scanner.nextInt();
        sum=x+sum;
        total++;

    }
        System.out.println("总和:"+sum);
        System.out.println("平均数:"+sum/total);
    }
}

运行结果:
请输入图片描述

顺序结构

顺序结构是最简单的基本结构,就是按顺序执行每一条语句。

if选择结构

if单选结构:

if(判断条件)
 {
  方法体
 }

代码演示:

import java.util.Scanner;
public class if01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
if (s.equals("Hello")){
    System.out.println(s);
}
        System.out.println("End");
    }
}

运行结果:
当输入Hello时,满足if条件,执行输出语句,输入的内容不是Hello时不满足if条件,输出End
请输入图片描述
请输入图片描述
if双选结构:

if(判断条件)
 {
 方法体
 }else
 {
 方法体
 }
import java.util.Scanner;

public class if01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
if (s.equals("Hello")){
    System.out.println(s);
}else
        System.out.println("End");
    }
}

运行结果:输入为Hello时输出s,输入内容不是Hello时输出End
请输入图片描述
if多选择结构与此类似,不再赘述。

switch选择结构

switch常与case联系在一起,是用来判断一个变量与一系列值中的某个值是否相等,每一个值成为一个分支。
switch语句中变量类型可以是byte,int,char,也可以是字符串。

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

运行结果为优秀。
注意要添加break,不然会发生穿透。

while循环

while(布尔表达式)
 {
方法体
 }

除此之外还有do-while循环

do{
 代码语句
 }
while(布尔表达式)

for循环

for语句的使用比较多,它可以使一些语句变得简单。
for循环是支持迭代的一种通用结构,是最有效最灵活的循环结构。
for循环执行次数是在执行前就确定的
举例:

for(int i=1,i<100,i++)
 {
 System.out.println(i);
 }

初始化语句可以为空,即int i=1这句可以为空。

练习题

1.计算0-100之间奇数和偶数的和

public class test01 {
    /* 分别计算1-100中偶数和奇数的和*/
    public static void main(String[] args) {
        int o = 0;
        int j = 0;
        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                o = o + i;
            } else {
               j=j+i;
            }
        }
        System.out.println("偶数和:"+o);
        System.out.println("奇数和:"+j);
    }
}

2.输出1-1000中能被5整除的数,并且每行输出3个

public class test02 {
    /*输出1-1000中能被5整除的数字,并且每行输出3个*/
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%15==0)
                System.out.println();
        }
    }
}

3.打印九九乘法表

public class test03 {
    /*打印九九乘法表*/
    public static void main(String[] args) {
        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();
        }
    }
}


continue和break的区别

break在任何循环语句的主体部分都可以控制循环流程,用于强行退出循环,不执行循环中剩余的语句
continue用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下次循环的判定。

continue代码举例:

public class continue1 {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i%2==0){
                continue;
            }else
                System.out.println(i);
        }
    }
}

运行结果:
请输入图片描述

break代码举例

public class break {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            if (i%2==0){
                break;
            }else
                System.out.println(i);
        }
    }
}

运行结果
请输入图片描述

无标签
打赏
评论区
头像