4.Java流程控制

zuojiawang / 2024-04-20 / 原文

Java流程控制

Scannner对象

(获取用户的输入)

//基本格式:
 Scanner scanner = new Scanner(System.in);
 String str = scanner.nextLine();//其中nextLine()按情况替换
 scanner.close();   
    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 static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);//alt+enter自动补全
        System.out.println("使用nextLine方法接收:");//以回车为结束符

        if(scanner.hasNextLine())
        {
            String str = scanner.nextLine();
            System.out.println("输出的内容:" +str);

        }
        scanner.close();
    }


    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);//alt+enter自动补全
        System.out.println("请输入数据:");
        String str = scanner.nextLine();
        System.out.println("输出的内容:" +str);
        scanner.close();
    }


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("输入的不是整数数据!");
        }

        System.out.println("请输入小数:");
        if(scanner.hasNextFloat())
        {
            f = scanner.nextFloat();
            System.out.println("小数数据:" + f);
        }else{
            System.out.println("输入的不是小数数据!");
        }



        scanner.close();
    }


 public static void main(String[] args) {
        //求总和和平均数
        Scanner scanner = new Scanner(System.in);
        double sum = 0;
        int count = 0;
        //通过循环判断是否还有数字
        System.out.println("请输入数据(enter分割,非数字结束)");
        while(scanner.hasNextDouble())
        {
            double x = scanner.nextDouble();
            count++;
            sum+=x;
        }
        System.out.println("和为:"+sum);
        System.out.println("平均值为:"+(sum/count));

        scanner.close();
    }


顺序结构

    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");
        System.out.println("Hello6");
    }


选择结构

1. if单选择结构

2. if双选择结构

3. if多选择结构

//if多选择结构
public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容:");
        int score = scanner.nextInt();
        /*
         if语句至多有一个else语句,else语句在所有的else if语句之前
         if语句可以有若干个else if,必须在else之前
         一旦其中一个else if 语句检测为true,跳过其他的else if 和 else
         */
        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();
    }

switch多选择结构

public static void main(String[] args) {
        //case 穿透 //switch 匹配一个具体的值
        char grade = 'C';
        switch(grade)
        {
            case'A':
                System.out.println("优秀");
                break;
            case'B':
                System.out.println("良好");
                break;
            case'C':
                System.out.println("及格");//如果没有break,则会穿透,下面选项全部输出
                break;
            case'D':
                System.out.println("再接再厉");
                break;
            case'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");

        }
    }
public static void main(String[] args) {
        //JDK7新特性:字符串也可以
        //字符串本质还是数字
        //反编译 java -- class ----IDEA

        String name = "Bob";
        switch (name)
        {
            case"Mike":
                System.out.println("Mike");
                break;
            case"Bob":
                System.out.println("Bob");
                break;
            default:
                System.out.println("I dont Know!");
        }
    }

反编译学习(查看源码)



循环结构

主要循环结构:

while循环

dowhile循环

for循环

while循环

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


public static void main(String[] args) {
        //死循环
        while(true)
        {
            //等待客户端连接
            //定时检查
            //。。。。。
        }
    }

 public static void main(String[] args) {
        //计算1+2+3+...+100=?
        int i=0;
        int sum=0;
        while(i<=100)
        {
            sum+=i;
            i++;
        }
        System.out.println(sum);
    }

do...while

 //while和do...while的区别
    //while是先判断后执行,do...while是先执行后判断
    //do...while总是保证循环体会被 至少执行一次!这是主要差别。
    public static void main(String[] args) {
        int i=0;
        int sum=0;
        do{
            sum+=i;
            i++;
        }while(i<=100);
        System.out.println(sum);
    }

5050

public static void main(String[] args) {
        //do...while总是保证循环体会被 至少执行一次!这是主要差别。
        int a=1;
        while(a<0)
        {
            System.out.println(a);
            a++;
        }
        System.out.println("***************");
        do {
            System.out.println(a);
            a++;
        }while (a<0);
    }
//输出:
***************
1

For循环

 public static void main(String[] args) {
        int a=1;//初始化条件
        while(a<=100)//条件判断
        {
            System.out.println(a);//循环体
            a+=2;//迭代
        }
        System.out.println("whlie 循环结束");
        //初始化条件//条件判断//迭代
        for (int i = 1; i < 100; i+=2) {
            //循环体
            System.out.println(i);
        }
        System.out.println("for循环结束");
        //100.for快捷生成:
        for (int i = 0; i < 100; i++) {

        }
        //注意:
        //最先执行初始化步骤,可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句;
        //然后,检测布尔表达式的值,如为true,循环体被执行,false则终止
        //执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)
        //再次检测布尔表达式,循环执行上面的过程。
        //死循环
//        for(;;){
//
//        }
    }


 public static void main(String[] args) {
        //计算0-100奇数偶数的和
        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
    
     public static void main(String[] args) {
        //输出1-100能被5整除的数
        for (int i = 1; i <= 100; i++) {
            if(i%5==0)
            {
                System.out.print(i+"\t");
            }
            if(i%(5*3)==0){
                System.out.println();
                //System.out.print("\n");
            }
        }
        //println 输出完会换行
        //print 输出完不会换行
    }
5	10	15	
20	25	30	
35	40	45	
50	55	60	
65	70	75	
80	85	90	
95	100	

    public static void main(String[] args) {
     //输出9*9乘法表
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i+"*"+j+"="+(j*i)+"\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	

    
    //增强for循环
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};
        for (int i = 0; i < 5; i++) {
            System.out.print(numbers[i]+"\t");
        }
        System.out.println();
        System.out.println("****************");
        //增强for循环
        for(int x:numbers)
        {
            System.out.print(x+"\t");
        }
    }

break continue

 public static void main(String[] args) {
        //用于强制退出循环
        int i=0;
        while(i<100)
        {
            i++;
            System.out.println(i);
            if(i==10)//运行到10,就跳出循环
            {
                break;
            }
        }
        System.out.println("123");
    }
1
2
3
4
5
6
7
8
9
10
123
 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);//不会输出10的倍数的数
        }
    }
123456789
111213141516171819
212223242526272829
313233343536373839
414243444546474849
515253545556575859
616263646566676869
717273747576777879
818283848586878889
919293949596979899

练习

public static void main(String[] args) {
    //打印三角形 5行
    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();
    }

}