JAVA基础的巩固_18/11/3(努力,奋斗)

1、switch中表达式值类型可以是byte、short、int、char、enum(枚举)、String(1.7以后可以)。不能用Long、float、double。

2、java中如何跳出当前的多重循环?

     (1)直接用return

     (2)带标签的break(这里附带记住带标签的break和带标签的continue的区别)

     (3)try/catch方法,在循环体内抛出异常,catch到后跳出所有循环

     (4)外循环的循环条件收到内循环代码的控制
        Boolean state =  true;
        for (int i = 1; i <= 4 && state; i++) {
            String a2 = "外层循环第"+i+"层";
            for (int j = 1; j <= 4 && state; j++) {
                String b2 = "内层循环第"+j+"层";
                if (2 == j & 2 == i) {
                    state = false;
                }
            }
        }

3、闰年的判断

     /**
     * if-else实现闰年的判断
     * 
     * 闰年的条件(或)
     * 1、能被4整除,但不能被100整除
     * 2、能被400整除
     */
    @SuppressWarnings("resource")
    public void years() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个年份");
        Long year = sc.nextLong();
        if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            System.out.println("该年是闰年");
        }else{
            System.out.println("该年不是闰年");
        }
    }

4、打印倒三角*

     /**
     * while打倒直角三角*
     * 外循环控制行数,内循环控制一行*的个数
     */
    public void triangle() {
        int a = 0;
        while(a < 5) {
            int b = a;
            while(b++ < 5) {
                System.out.print("*");
            }
            System.out.println();
            a++;
        }
    }

猜你喜欢

转载自blog.csdn.net/EricUUU/article/details/83690599