java入门篇5 --- 流程控制

1.输入与输出

关于输入与输出,字符串格式化,对应如下:

import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        // String:我我,int10,float:10.234000,科学计数:1.000000e+05
        // %s格式化字符串, %d格式化整型,%f格式化浮点数,%e将浮点数格式化为科学计数法,注意对应的值必须是浮点数
        System.out.printf("String:%s,int%d,float:%f,科学计数:%e", "我我", 10, 10.234, 100000.00);
        System.out.println("你多大?");
        Scanner scanner = new Scanner(System.in);  // 这就是相当于捕捉系统输入
        String name = scanner.nextLine();  // 将捕捉到的信息复制给name,字符串类型
        System.out.println("what's your name?");
        String age = scanner.nextLine();
        System.out.print(name);  // println 是print line, 与print区别就是是否是行级输出,这个就是name跟age在一行
        System.out.print(age);  // 25yang
    }
}

2.if判断

if判断基本结构是

if(条件){...}else if(条件){...}else{...}

public class HelloWorld {
    public static void main(String[] args) {
        int score = 90;
        if (score > 90) {
            // 判断成绩是否大于90
            System.out.println("优秀");
        } else {
            // 成绩小于90,走这个
            if (score > 80) {
                // 判断城市是否大于80
                System.out.println("良好");
            } else if (score > 70) {
                System.out.println("一般");
            } else {
                if (score > 60) {
                    System.out.println("及格");
                } else {
                    System.out.println("不及格");
                }
            }
        }
    }
}

3.switch语句

switch语句每个判断条件下一定要加break,否则如果一个条件判断成功,则会执行所有的语句,另外建议最后加一个default,意思是所有都没有判定成功,就执行这个语句块

public class HelloWorld {
    public static void main(String[] args) {
        int score = 90;
        switch (score){
            case 90:
                System.out.println("90");
                break;
            case 80:
                System.out.println("80");
                break;
            case 70:
                System.out.println("70");
                break;
            case 60:
                System.out.println("60");
                break;
            default:
                System.out.println("not found");
                break;
        }
    }
}

4.while与do while

while是先进行条件判断,然后在执行包裹的语句块,do{}while是先执行包裹的语句块一遍,然后进行条件判断,决定知否继续执行,

因此“System.out.println("我执行啦");”,这个条件不满足,没有执行,

虽然score>10也不满足,但是“System.out.println("score等于5");”执行了

public class HelloWorld {
    public static void main(String[] args) {
        int score = 90;
        while (score < 90) {
            System.out.println("我执行啦");
        }
        while (score > 5) {
            score--;
        }
        System.out.println(score);  // 5
        do {
            System.out.println("score等于5");  // score等于5
        } while (score > 10);
    }
}

5.for循环

for循环跟js语法结构一样,同时java的for循环也可以使用for each操作

public class HelloWorld {
    public static void main(String[] args) {
        int[] a = new int[]{1, 2, 3, 4, 5};
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);  // 1 2 3 4 5
        }
     // for each
for (int i : a) { System.out.println(i); // 1 2 3 4 5 } } }

7.continue与break一个是跳出此次循环继续执行,break是打断本次循环

6. 数组操作:

1).数组循环可以使用for循环

2).数组内部元素可以是各种,例如多维数组

public class HelloWorld {
    public static void main(String[] args) {
        // 二维数组
        int[][] a = new int[2][3];
        a[0][0] = 1;
        a[0][1] = 2;
        a[0][2] = 3;
        a[1][0] = 4;
        a[1][1] = 5;
        a[1][2] = 6;
        for (int[] i : a) {
            for (int j : i) {
                System.out.println(j);  // 1 2 3 4 5 6
            }
        }
    }
}
posted @ 2020-01-06 23:15  灬灬灬灬灬灬  阅读(148)  评论(0编辑  收藏  举报