逻辑控制——JavaSE

1.顺序结构

按照代码的顺序执行,代码的书写顺序发生改变,则执行的顺序也会发生变化

    public static void main(String[] args) {
        System.out.println("aaa");
        System.out.println("bbb");
        System.out.println("ccc");
    }

2.分支结构

2.1 if语句

1.语法格式

语法格式1
if(布尔表达式){
//语句
}
语法格式2
if(布尔表达式){
//语句1
}else{
//语句2
}
语法格式3
if(布尔表达式1){
//语句1
}else if(布尔表达式2){
//语句2
}else{
语句3
}

注意:
①只要满足其中某一个情况就执行里面的语句块,但是这三个语句块不可能同时执行
②建议以后写if语句的时候,所有的花括号都得加上—>增加代码的可读性
2.练习
①判断一个数字是奇数还是偶数

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//键盘输入
        //Ctrl+D 结束循环
        while (scanner.hasNextInt()) {
            int num = scanner.nextInt();
            if (num % 2 == 0 ) {
                System.out.println("num是偶数");
            }else{
                System.out.println("num是奇数");
            }
        }
    }

②判断一个数字是正数、负数、还是零

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextInt()){
            int num = scanner.nextInt();
            if(num > 0) {
                System.out.println("num是正数");
            }else if(num < 0){
                System.out.println("num是负数");
            }else{
                System.out.println("num是零");
            }
        }
    }

③判断一个年份是否为闰年

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int year = scanner.nextInt();
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    System.out.println(year + " is lespYear");
                } else {
                    System.out.println(year + " is not lespYear");
                }
            } else {
                if (year % 4 == 0) {
                    System.out.println(year + " is lespYear");
                } else {
                    System.out.println(year + " is not lespYear");
                }
            }
        }
    }

3.悬垂else问题
注意就好,一条语句也建议加上大括号

2.2 switch语句

    public static void main(String[] args) {
        int day = 1;
        switch(day) {
            case 1:
                System.out.println("周一");
                break;
            case 2:
                System.out.println("周二");
                break; 
            case 3:
                System.out.println("周三");
                break;            
            case 4:
                System.out.println("周四");
                break;
            case 5:
                System.out.println("周五");
                break;            
            case 6:
                System.out.println("周六");
                break;
            case 7:
                System.out.println("周日");
                break;
            default:
                System.out.println("输入有误");
                break;
        }
    }

注意:
①每个case和default后面最好都加上break
②if和Switch都可以达到选择的效果,实际上应用谁都可以
③不能做Switch的参数的类型:float、double、long、Boolean
④string和枚举都可以做为Switch的参数,byte、char、short、int也都可以
⑤Switch后面括号所带的参数不可以是复杂表达式,例如:day > 2等等
⑥Switch是支持嵌套的
⑦多个case后的常量值不可以重复

3.循环结构

3.1 while循环

1.语法格式:

while(布尔表达式){
//循环语句
}

2.练习
①打印1-10的数字

    public static void main(String[] args) {
        int num = 1;
        while(num <= 10){
            System.out.println(num);
            num++;
        }
    }

②计算1-100的和

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ret = 0;
        int i = 1;
        while(i <= n){
            ret += i;
            i++;
        }
        System.out.println(ret);
    }

③计算5的阶乘

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ret = 1;
        int i = 1;
        while(i <= n){
            ret *= i;
            i++;
        }
        System.out.println(ret);
    }

④计算1!+2!+3!+4!+5!

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i = 1;
        int sum = 0;
        while(i <= n){
            int j = 1;
            int ret = 1;
            while(j <= i){
                ret *= j;
                j++;
            }
            sum += ret;
            i++;
        }
        System.out.println(sum);
    }

⑤写一个死循环

    public static void main(String[] args) {
        while(true){
            System.out.println("fefe");
        }
    }

3.2 break

中断的意思,存在于循环中,Switch是个例外,在Switch中也可以有break。break表示结束整个循环,实例如下:找到100-200中第一个是三的倍数的数字

    public static void main(String[] args) {
        int num = 100;
        while(num <= 200){
            if(num % 3 == 0){
                System.out.println(num);
                break;
            }
            num++;
        }
    }

3.3 continue

结束本趟循环实例如下:找到100-200之间所有3的倍数

    public static void main(String[] args) {
        int num=100;
        while(num<=200){
            if(num%3!=0){
                num++;
                continue;
            }
            System.out.println(num);
            num++;
        }
    }

找到1-100之间既是3的倍数,又是5的倍数的所有数字

    public static void main(String[] args) {
        int num = 1;
        while(num <= 100){
            if(num % 3!= 0||num %5!=0){
                num++;
                continue;
            }
            System.out.println(num);
            num++;
        }
    }

3.4 for循环

1.语法格式

for(表达式1;布尔表达式2;表达式3){
语句块
}

执行流程:
表达式1相当于是对循环条件的一个初始化
第一次循环:表达式1->表达式2->语句块->表达式3
第二次循环: 表达式2->语句块->表达式3
2.for的死循环:

    public static void main(String[] args) {
        int i = 1;
        for (; ; i++) {
            System.out.println("ads");
        }
    }

第一个分号和第二个分号之间的语句表示判断,没有判断的话,就永远为真

3.5 do while循环

1.语法格式

do{
语句块
}while(循环条件);//注意这个分号,不要丢掉

2.do while 循环至少会执行一次,一般不建议使用这个循环处理问题

4.输入输出

4.1输出

基本语法:

    public static void main(String[] args) {
        System.out.println("sdc");//输出换行
        System.out.print("sdc");//输出不换行
        System.out.printf("%s\n","dokjv");//和C语言一样的输出,不建议使用
    }

4.2从键盘输入

使用scanner读取

import java.util.Scanner;//导入包,在最开头
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//资源
        System.out.println("请输入你的姓名:");
        String name = scanner.nextLine();
        System.out.println(name);
        System.out.println("请输入你的年龄:");
        int age = scanner.nextInt();
        System.out.println(age);
        System.out.println("请输入你的成绩:");
        float grades = scanner.nextFloat();
        System.out.println(grades);
        scanner.close();//关闭方法,记住

    }

String name = scanner.next();

遇到空格结束,默认空格后面的输入为你下一个要输入的数据,同时,尽量不要在nextline上面使用nextInt和nextFloat等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值