Day09

Day09

顺序结构

package struck;
​
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");
    }
}

if单选择结构

if双选择结构

package struck;
​
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();
    }
}

if多选择结构

package struck;
​
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("成绩不合法");
        }
​
        scanner.close();
    }
}

嵌套的if结构

switch多选择结构

package struck;
​
public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透  //switch 匹配一个具体的值
        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;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");
        }
    }
}

javaSE 7开始switch支持字符串string类型

package struck;
​
public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "DHC";
        //JDK7的新特性,表达式结果可以是字符串
        //字符串的本质还是数字
​
        //反编译 java---class(字节码文件)----反编译(IDEA)
​
        switch (name){
            case "DHC":
                System.out.println("DHC");
                break;
            case "ABC":
                System.out.println("ABC");
                break;
            default:
                System.out.println("NULL");
        }
    }
}

循环结构

1.while循环

package struck;
​
public class WhileDemo01 {
    public static void main(String[] args) {
​
        //输出1~100
​
        int i = 0;
​
        while (i<100){
            i++;
            System.out.println(i);
        }
​
​
    }
}
package struck;
​
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);
    }
}

2.do...while循环

package struck;
​
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 struck;
​
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);
        //输出0
    }
}

3.for循环

for循环先执行里面循环的再进行迭代!!!

package struck;
​
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循环结束");
​
    }
}
package struck;
​
public class ForDemo02 {
    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;
            }else{
                evenSum+=i;
            }
        }
​
        System.out.println("奇数的和"+oddSum);
        System.out.println("偶数的和"+evenSum);
    }
}
package struck;
​
public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1-100之间能被5整除的输,并且每行输出3个
​
        for (int i = 0; i < 100; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){//每行
                System.out.println();
                //System.out.print("\n");
            }
        }
        int j = 0;
        while (j<=100){
            if (j%5==0){
                System.out.print(j+"\t");
            }
            if (j%(5*3)==0){
                System.out.println();
            }
            j++;
        }
​
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值