Day6(循环语句学习)

本文详细介绍了Java中的if条件判断、switch多选项选择和while循环的基本用法,通过实例演示如何根据分数输出等级,并展示了如何使用switch处理字符串情况。同时涵盖了while循环计算和控制打印格式。
摘要由CSDN通过智能技术生成

循环语句学习

if循环

package com.xxcsh.www.struct;

import java.util.Scanner;

public class ifdemo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double score = scanner.nextDouble();

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

    }
}

switch循环

package com.xxcsh.www.struct;


public class switchdeemo1 {
    public static void main(String[] args) {
        //case穿透
        String name = "xxcsh";
        switch (name){
            case "xxcsh":
                System.out.println("xxcsh");
            break;
            case "中华":
                System.out.println("中华");
            break;
            default:
                System.out.println("输入有误!");


        }
    }
}

while循环

package com.xxcsh.www.struct;

public class whiledemo1 {
    public static void main(String[] args) {
        //计算1~100
        int i = 0;
        int sum = 0;


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

    }
}

package com.xxcsh.www.struct;

public class whiledemo2 {
    public static void main(String[] args) {
        //1000以内能被五整除的数,每三个换一行
        int i = 0;
        while (i<=1000) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.print("\n");
            }
            i++;

        }
        System.out.println(i);
    }



}

for循环

package com.xxcsh.www.struct;

public class fordemo1 {
    public static void main(String[] args) {
        //1000以内能被五整除的数,每三个换一行
        for (int i = 0; i <=1000; i++) {
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
            }

        }
    }
}

package com.xxcsh.www.struct;

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

```java
package com.xxcsh.www.struct;

public class test {
    //打印三角形    五行
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
            /*
            输出结果为
                  *
                 ***
                *****
               *******
              *********
              
             */
        }
    }
}

Test

package com.xxcsh.www.struct;

public class test {
    //打印三角形    五行
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {
                System.out.print("*");
            }
            System.out.println();
            /*
            输出结果为
                  *
                 ***
                *****
               *******
              *********
              
             */
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值