Day04__Java流程控制

Java流程控制

用户交互Scanner

image-20220902151906525

image-20220902153343376

package processControl;

//用户交互Scanner

import java.util.Scanner;

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

        System.out.println("使用nextLine方式接收:");
        if (scanner.hasNextLine()){
            //使用nextLine方式接收
            scanner.nextLine();//提前吞掉空格,防止后面吞掉
            String str = scanner.nextLine();
            System.out.println("输出的内容为:"+str);
        }

        //凡是属于IO流的类如果不关闭会一直占用资源
        scanner.close();
    }
}

注:

(13条消息) java——Scanner中nextLine()方法和next()方法的区别_super-yong的博客-CSDN博客_nextline()

(13条消息) Java Scanner类的使用(以及nextLine方法吞回车的解决方法)_SummerNH_6的博客-CSDN博客_scanner 回车结束

Scanner的进阶使用

package processControl;

import java.util.Scanner;

//Scanner的进阶使用
public class Demo02 {
    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(scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }
        else{
            System.out.println("输入的不是小数");
        }
        test01();
        scanner.close();
    }

    public static void test01(){
        //输入多个数字,求其总和平均数,每输入一个数字用回车确认,用非数字确认结束执行
        Scanner scanner = new Scanner(System.in);
        //总和
        double sum = 0;
        //计算输入了多少个数字
        int m = 0;
        //通过循环判断是否还有输入,并在里面对每一次进行求和统计
        while(scanner.hasNextDouble()){
            double x = scanner.nextDouble();
            m+=1;
            sum+=x;
        }
        System.out.println(m+"个数字和为"+sum);
        System.out.println(m+"个数字平均值为"+sum/m);

        scanner.close();
    }

}

顺序结构

image-20220903162401497

package processControl;

//顺序结构
public class Demo03 {
    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选择结构

package processControl;

import java.util.Scanner;

//if选择结构
public class Demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        //if单选
        System.out.println("请输入内容");
        String s =scanner.nextLine();
        //equals:判断字符串是否相等
        if(s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("End");


        //if双选
        System.out.println("请输入成绩");
        int sccore = scanner.nextInt();
        if(sccore>=60){
            System.out.println("及格");
        }else {
            System.out.println("不及格");
        }


        //if多选
        System.out.println("请输入选择");
        int i = scanner.nextInt();
        if(i==1){
            System.out.println("选择了1");
        } else if (i==2) {
            System.out.println("选择了2");
        }else {
            System.out.println("没有选择1和2");
        }


        //if嵌套
        System.out.println("请输入1-3的一个数");
        int num = scanner.nextInt();
        if(num<=3 && num>=0){
            if(num==1) {
                System.out.println("你输入了1");
            } else if (num==2) {
                System.out.println("你输入了2");
            }else {
                System.out.println("你输入了3");
            }
        }else{
            System.out.println("你没有按规则输入");
        }

        scanner.close();
    }
}

switch选择结构

image-20220903164638260

package processControl;

import java.util.Scanner;

//switch选择结构
public class Demo05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩等级(A,B,C,D,E)");
        char grade = scanner.next().charAt(0);
        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("不及格");
            case 'E':
                System.out.println("非常差");
            default:
                System.out.println("输入错误");
        }

    }
}

image-20220903165815183

while循环结构

image-20220903170025228

image-20220903170102601

image-20220903170432084

image-20220903170723019

package processControl;

//while循环结构
public class Demo06 {
    public static void main(String[] args) {
        //输出1-10
        //while循环
        int i = 0;
        while(i<10){
            i++;
            System.out.println(i);
        }


        //do...while循环
        i = 0 ;
        int sum = 0;
        do{
            sum+=i;
            i++;
        }while(i<=100);
        System.out.println("0-100的和为"+sum);


        //for循环
        //99乘法表
        for(i = 1 ; i <= 9 ; i++){
            for(int j = 1 ; j <= i ; j++) {
                System.out.print(j+"*"+i+"="+i*j+"\t");
            }
            System.out.println();
        }

    }
}

增强for循环

image-20220903172007907

package processControl;

//增强for循环
public class Demo07 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义数组
        //便利数组的元素
        for (int x:numbers){
            System.out.println(x);
        }

    }
}

break;continue;goto

image-20220903181515424

package processControl;

//break;continue;goto
public class Demo08 {
    public static void main(String[] args) {
        //break  //跳过整个循环
        for (int i = 0; i < 100; i++) {
            System.out.println(i);
            if(i==10){
                break;
            }
        }

        //continue  //跳过本次循环
        for (int i = 0; i < 10; i++) {
            if(i%2==0){
                continue;
            }
            System.out.println(i);
        }

        //goto(了解)
        //打印101-150之间所有的质数
        int count = 0;
        outer:for (int i=101 ; i<150 ; i++){
            for(int j=2 ; j<i/2 ; j++){
                if(i%j==0){
                    continue outer;
                }
            }
            System.out.println(i);
        }

    }
}

打印三角形及Debug

package processControl;

//打印三角形及Debug
public class Demo09 {
    public static void main(String[] args) {
        for (int i = 0; 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();
        }
    }
}

image-20220903183331845

posted @ 2022-09-03 18:36  不迷路的小孩  阅读(17)  评论(0编辑  收藏  举报