基础09结构

结构

顺序结构

package com.hao.struct;

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

选择结构

if选择

package com.hao.struct;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入内容");
        String s = sc.nextLine();
        //equals:判断字符串是否相等
        if (s.equals("hello")) {		//单选择
            System.out.println(s);
        }
        System.out.println("end");
        sc.close();
    }
}
package com.hao.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //分数达到60就是及格,小于60就是不及格
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入成绩");
        int a = sc.nextInt();
        if(a>=60){							//双选择
            System.out.println("成绩合格");
        }else {
            System.out.println("成绩不合格");
        }
    }
}
package com.hao.struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double a = sc.nextDouble();
        if (a == 100) {					//多选择
            System.out.println("恭喜满分");
        } else if (a < 100 && a >= 90) {
            System.out.println("A");
        } else if (a < 90 && a >= 80) {
            System.out.println("B");
        } else if (a < 80 && a >= 70) {
            System.out.println("C");
        } else if (a < 70 && a >= 60) {
            System.out.println("D");
        } else if (a < 60 && a >= 0) {
            System.out.println("成绩不合格");
        } else {
            System.out.println("成绩不合法");
        }
    }
}

switch 选择

package com.hao.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        char grade = 'C';
        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.hao.struct;

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("未知");
        }



    }
}

循环结构

while

package com.hao.struct;

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

死循环,不要尝试

package com.hao.struct;

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

计算前100和

package com.hao.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+3+...+100
        int a = 0;
        int sum = 0;
        while (a < 100) {
            a++;
            sum = sum + a;
        }
        System.out.println(sum);
    }
}

do{…}while()

package com.hao.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int a = 0;
        int sum = 0;
        do {
            a++;
            sum = sum + a;
        } while (a < 100);
        System.out.println(sum);
    }
}
package com.hao.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);
            a++;
        }while(a<0);
    }
}

for循环

package com.hao.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int a = 0;
        while (a < 10) {
            a++;
            System.out.println(a);
        }
        System.out.println("============================");
        for (int i = 0; i <= 10; i++) {                //100.for 回车  直接生成
            System.out.println(i);
        }
    }
}

练习1 : 分别输出1~100的奇偶数和

package com.hao.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        // 练习1 :  分别输出1~100的奇偶数和
        int oddSum = 0;
        int evenSum = 0;
        for (int i = 0; i <= 100; i++) {
            if (i%2!=0){                //  奇数
                oddSum= oddSum + i;
            }else {                     // 偶数
                evenSum= evenSum+ i;
            }
        }
        System.out.println(oddSum);
        System.out.println(evenSum);
    }
}

练习2: 用while或for输出1~1000之间能被5整除的数,并且每行输三个

package com.hao.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //练习2:   用while或for输出1~1000之间能被5整除的数,并且每行输三个

        for (int i = 0; i <= 1000; i++) {
            if (i % 5 == 0) {
                System.out.print(i + "\t");   //println 输出完会换行   \t 转义字符,相当于tab
                if (i % 15 == 0) {                //print  输出完不会换行
                    System.out.println();
                    //System.out.print("\n");
                }
            }
        }
    }
}

练习3 九九乘法表

package com.hao.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        //练习3  九九乘法表
        for (int i = 1; i <= 9; i++) {
            for (int i1 = 1; i1 <= i; i1++) {
                System.out.print(i + "*" + i1 + "=" + i * i1 + "\t");
            }
            System.out.println();
        }
    }
}

增强for循环

package com.hao.struct;

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);
        }
    }
}

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

package com.hao.struct;

public class BreakDemo {
    public static void main(String[] args) {
        //break在任何循环语句的主体部分,均可用break控制循环的流程。
        //break用于强行退出循环,不执行循环中剩余的语句。(break语句也在switch语句中使用)
        int i = 0;
        while (i < 100) {
            i++;
            System.out.println(i);
            if (i == 30) break;
        }
        System.out.println("123");
    }
}

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

package com.hao.struct;

public class ContinueDemo {
    public static void main(String[] args) {
        //continue语句用在循环语句体中,用于终止某次循环过程
        //即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定
        int a = 0;
        for (int i = 0; i < 100; i++) {
            if (i % 10 == 0) {
                System.out.println();
                continue;
            }
            System.out.print(i + "\t");
        }
    }
}

打印三角形 五行

package com.hao.struct;

public class TestDemo {
    public static void main(String[] args) {
        //打印三角形  5行
        for (int i = 0; i < 5; i++) {
            for (int j = 5; j > i; j--) {   //先输出空格位置
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++) {  //再输出一半  *
                System.out.print("*");
            }
            for (int l = 0; l < i; l++) {  //输出另一半  *
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值