Java中的顺序结构、选择结构和循环结构

package com.ming.struct;

/**
 * @ClassName: ShunXuDemo
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/14-22:48
 * @Description: 01顺序结构
 */
public class ShunXuDemo {
    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");
    }
}

package com.ming.struct;

import java.util.Scanner;

/**
 * @ClassName: IfDemo01
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/14-23:08
 * @Description: 01 if选择结构
 */
public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入内容:");
        String s = scanner.nextLine();

        //equals:判断字符串是否相等(这里不能使用 ==)
        if(s.equals("Hello")){
            System.out.println(s);
        }

        System.out.println("End");
        scanner.close();
    }
}

package com.ming.struct;

import java.util.Scanner;

/**
 * @ClassName: IfDemo02
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/15-0:27
 * @Description: 02 if双选择结构
 *
 * * 现在有个需
 */
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.ming.struct;

import java.util.Scanner;

/**
 * @ClassName: IfDemo03
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/15-0:47
 * @Description: 03 if多选择结构
 */
public class IfDemo03 {
    public static void main(String[] args) {
        //考试分数大于60就是及格,小于60分就不及格。
        Scanner scanner = new Scanner(System.in);

        /*
        if语句至多有一个else语句,else语句在所有的else if语句之后。
        if语句可以有若干个else if语句,它们必须在else语句之前。
        一旦其中一个else if语句检测为true,其他的else if以及else语句都将跳过执行。
         */

        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("成绩不合法");
        }


        scanner.close();
    }
}

package com.ming.struct;

/**
 * @ClassName: SwitchDemo01
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/15-16:50
 * @Description: 01 Switch选择结构
 */
public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透    //switch匹配一个具体的值
        char grade = 'F';

        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; //可选
            case 'E':
                System.out.println("挂科");
                break; //可选
            default: //可选  不在上述情况中
                System.out.println("未知等级");
        }
    }
}

package com.ming.struct;

/**
 * @ClassName: SwitchDemo02
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/15-17:27
 * @Description: 02 switch支持字符串String类型
 */
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "秦疆";
        //JDK7的新特性,表达式结果可以是字符串!!!
        //字符的本质还是数字

        //反编译   java---class(字节码文件)----反编译(IDEA)

        switch (name){
            case "秦疆":
                System.out.println("秦疆");
                break;
            case "狂神":
                System.out.println("狂神");
                break;
            default:
                System.out.println("弄啥嘞!");
        }
    }
}

package com.ming.struct;

/**
 * @ClassName: WhileDemo01
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/15-21:38
 * @Description: 01 while循环
 */
public class WhileDemo01 {
    public static void main(String[] args) {

        //输出1~100

        int i = 0;

        while (i<100);
        i++;
        System.out.println(i);
    }
}

package com.ming.struct;

/**
 * @ClassName: WhileDemo02
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/15-21:42
 * @Description: 02死循环
 */
public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while (true){
            //等待客户端连接
            //定时检查
            //。。。。。。。
        }
    }
}

package com.ming.struct;

/**
 * @ClassName: WhileDemo03
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/15-21:57
 * @Description: 03 计算1+2+3+…+100=?
 */
public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+…+100=?

        //高斯的故事

        int i = 0;
        int sum = 0;

        while (i<=100){
            sum = sum + i;
            i++;
        }

        System.out.println(sum);
    }
}

package com.ming.struct;

/**
 * @ClassName: DoWhileDemo01
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/16-1:49
 * @Description: 01 Do...While循环
 */
public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do {
            sum = sum + i;
            i++;
        }while (i<=100);    //保证循环体会至少被执行一次

        System.out.println(sum);
    }
}

package com.ming.struct;

/**
 * @ClassName: DoWhileDemo02
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/16-2:04
 * @Description: 02 while循环 和 do...while循环对比
 */
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);
            a++;
        }while (a<0);
    }
}

package com.ming.struct;

/**
 * @ClassName: ForDemo01
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/16-17:48
 * @Description: 01 for循环
 */
public class ForDemo01 {
    public static void main(String[] args) {
        int a =  1; //初始化条件

        while (a<=100){ //条件判断
            System.out.println(a); //循环体
            a+=2; //迭代
        }

        System.out.println("while循环结束!");

        //初始化//条件判断//迭代
        for (int i=1;i<=100;i++){
            System.out.println(i);
        }

        System.out.println("for循环结束!");

        /*
        关于for循环有以下几点说明:

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

        //死循环
        for (; ; ) {

        }
    }
}

package com.ming.struct;

import javax.xml.transform.Source;

/**
 * @ClassName: ForDemo02
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/16-21:55
 * @Description: 02 计算0到100之间的奇数和偶数的和
 */
public class ForDemo02 {
    public static void main(String[] args) {
        //练习1:计算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;
            }
        }

        System.out.println("奇数的和"+oddSum);
        System.out.println("偶数的和"+evenSum);
    }
}

package com.ming.struct;

/**
 * @ClassName: ForDemo03
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/17-0:54
 * @Description: 03 用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
 */
public class ForDemo03 {
    public static void main(String[] args) {
        //练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

        //for循环
        for (int i = 0; i <= 1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
                //System.out.println("\n");
            }
        }

        //println 输出完会换行
        //print 输出完不会换行

        //while循环
        int a = 0;
        while (a<=1000){
            if (a%5==0){
                System.out.print(a+"\t");//"\t"制表符   等同于 Tab
            }
            if (a%(5*3)==0){
                System.out.println("\n");//"\n"换行符  等同于 回车(Enter)
            }
            a++;
        }
    }
}

package com.ming.struct;

/**
 * @ClassName: ForDemo04
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/17-1:18
 * @Description: 04 打印九九乘法表
 */

/*
1x1=1
1x2=2  2x2=4
1x3=3  2x3=6   3x3=9
1x4=4  2x4=8   3x4=12  4x4=16
1x5=5  2x5=10  3x5=15  4x5=20  5x5=25
1x6=6  2x6=12  3x6=18  4x6=24  5x6=30  6x6=36
1x7=7  2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49
1x8=8  2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64
1x9=9  2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81
 */
public class ForDemo04 {
    public static void main(String[] args) {
        //练习3:打印九九乘法表

        //1.我们先打印第一列,这个这个大家应该都会
        //2.我们把固定的1再用一个循环包起来
        //3.去掉重复项,i <= j
        //4.调整样式

        //嵌套for循环
        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(i + "x" + j + "=" + (j * i) + "\t");
            }
            System.out.println();
        }
    }
}
package com.ming.struct;

/**
 * @ClassName: ForDemo05
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/17-18:13
 * @Description: 05 增强for循环
 */
public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义了一个数组


        for (int i = 0;i<5;i++){
            System.out.println(numbers[i]);
        }
        System.out.println("=====================");
        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

package com.ming.struct;

/**
 * @ClassName: BreakDemo
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/17-23:39
 * @Description: Break
 */
public class BreakDemo {
    public static void main(String[] args) {
        int i = 0;
        while (i<100){
            i++;
            System.out.println(i);
            if (i==30){
                break;
            }
        }
        System.out.println("123");
    }
}

package com.ming.struct;

/**
 * @ClassName: ContinueDemo
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/17-23:46
 * @Description: Continue
 */
public class ContinueDemo {
    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);
        }

        //break在任何循环语句的主体部分,均可用break控制循环的流程。
        //break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)                               [辞职]

        //continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。    [请假]
    }
}

package com.ming.struct;

/**
 * @ClassName: LabelDemo
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/18-0:10
 * @Description: (不需要掌握) goto关键字~~带标签的break和continue
 */
public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间所有的质数
        //质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。

        int count = 0;

        //不建议使用!
        outer:for (int i = 101;i<150;i++){
            for (int j = 2;j<i/2;j++){  // i如果有(除了1和本身)其他因数,一定在2到i/2之间
                if (i % j == 0){
                    continue outer;
                }
            }
            System.out.print(i+" ");
        }
    }
}

package com.ming.struct;

/**
 * @ClassName: TestDemo
 * @Author: 南冥有猫不须铭
 * @Date: 2021/3/18-0:44
 * @Description: 打印三角形及Debug
 */
public class TestDemo {
    public static void main(String[] args) {
        //打印三角形

        for (int i = 1;i<=5;i++){   //i是行,j是列
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");//最左边空格部分的直角三角形(这里要用print,不能用println自动换行)
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");//左半边三角形
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");//右半边三角形(比左边矮一行)
            }

            System.out.println();//即"\n"
        }

        /*  也可以
        for (int i = 1;i<=5;i++){
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2*i-1); j++) {
                System.out.print("*");
            }
            System.out.println();
        }
        */

        //Debug:在一行代码的最前面点红点,然后点工具栏的爬虫(运行后面那个),可以一行一行地查看运行信息
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值