代码结构

顺序结构

package com.study.struct;

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

选择结构

if单选择结构

package com.study.struct;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容:");
        String s = scanner.nextLine();
        //equas 判断字符串相等
        if(s.equals("hello")){
            System.out.println(s);
        }else {
            System.out.println("end");
        }
        scanner.close();

    }
}

package com.study.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //考试大于60分及格,小于60分不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        if(score>60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }

        scanner.close();
    }
}

package com.study.struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        int score = scanner.nextInt();
        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("成绩不合法");
        }
    }
}

switch多选择结构

package com.study.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade = 'A';
        // case穿透
        // switch 匹配一个具体值
        // 支持类型 byte short int char  javaSE 7之后,支持String类型
        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("再接再厉");
                break;
            default:
                System.out.println("挂科");

        }
    }
}

package com.study.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "张一";
        // jdk 7 新特性 支持字符串
        // 字符串的本质还是数字
        // 反编译   java ---class(字节码文件)-----反编译(idea)
        switch (name) {
            case "张一":
                System.out.println("张一");
                break;
            case "张二":
                System.out.println("张二");
                break;
            default:
                System.out.println("弄啥嘞!");


        }
    }
}

while 循环结构

while

1 先判断后执行

package com.study.struct;

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

package com.study.struct;

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

package com.study.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //1~100 求和
        int sum=0;
        int i=0;
        while (i<=100){
            sum+=i;
            i++;
        }
        System.out.println(sum);
    }
}

do。。。while

先执行,后判断;总是保证循环体会被至少执行一次!

package com.study.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        int sum=0;
        do {
            sum+=i;
            i++;
        }while (i<=100);
        System.out.println(sum);
    }
}

package com.study.struct;

public class DoWhileDemo02 {
    public static void main(String[] args) {
        int a=0;
        while (a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("~~~~~~~~~~~~~~~~~");
        do {
            System.out.println(a);
        }while (a<0);
    }
}

for循环

package com.study.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int i=1;  //初始化条件
        while (i<=100){//条件判断
            System.out.println(i); // 循环体
            i+=2;//迭代
        }
        System.out.println("while循环结束");
        for (int x=1;x<=100;x+=2){
            System.out.println(x);
        }
        System.out.println("for 循环结束");
        for (int i1 = 0; i1 < 100; i1++) {
            
        }
        // 死循环 没有初始值;没有表达式,默认true;没有迭代
        for (; ; ) {

        }


    }
}

package com.study.struct;

public class ForfDemo02 {
    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;//oddSum=oddSum+i;
            }else {//偶数
                evenSum+=i;//evenSum=evenSum+i;
            }
        }
        System.out.println("oddSum=:"+oddSum);
        System.out.println("evenSum=:"+evenSum);
    }
}

package com.study.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        // 练习:输出1~1000之间能被5整除的数,并且每行输出三个
        /*int x=0;
        for (int i = 1; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
                x++;
                if(x%3==0){
                    System.out.print("\n");
                }
            }
        }*/
        /*for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.print("\n");
            }
        }*/
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0&&i!=0){
                System.out.print(i+"\t");
                if (i%(5*3)==0){
                    System.out.print("\n");
                }
            }

        }
    }
}

package com.study.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        /*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 (int i = 1; i <= 9; i++) {
            for (int i1 = 1; i1 <= i; i1++) {
                System.out.print(i1 + "*" + i + "=" + i1 * i + '\t');
            }
            System.out.print('\n');
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值