Java day_02 循环while、for、switch

import java.awt.*;
import java.util.Scanner;

public class day_02 {
    public static void main(String[] args) {
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        test7();
        test8();
        test9();
        test10();
        test11();
        test12();
        test13();
        test14();
        test15();
        test16();
        test17();
        test18();
        test19();
        test20();
        test21();
    }

        public static void test1(){
//        循环条件初始化
        int i=1;
//        循环条件控制
        while(i<=5){
            System.out.println("Hello,World!");
//            循环条件改变
//            i++;
        }
    }
    public static void test2(){
        int i=1;
        int sum=0;
        while(i<=100){
            System.out.println(i);
            sum+=i;
            i++;
        }
        System.out.println(sum);
    }
    public static void test3(){
        int i=1;
        while(i<=9){
            System.out.println(i+"*9="+(i*9)+"\t");
            i++;
        }
    }
    public static void test4(){
        int random=(int)(Math.random()*1000+1);
        System.out.println(random);
        Scanner scanner=new Scanner(System.in);
        while(true){
            System.out.println("guess!");
            int num=scanner.nextInt();
            if(num>random){
                System.out.println("too big!");
            }else if(num<random&&num!=0){
                System.out.println("too samll!");
            }else if(num==0){
                System.out.println("come on!");
                break;
            }else{
                System.out.println("you win!");
                break;
            }
        }
        System.out.println("end");
    }
    public static void test5(){
        Scanner scanner=new Scanner(System.in);
        int password=0;
        do{
            System.out.println("Please Enter Password:");
            password=scanner.nextInt();
            System.out.println("The Password Is False");
        }while(password!=123);
        System.out.println("The Password Is True");
    }
    public static void test6(){
        int sum=0;
        for (int i=1;i<=100;i++){
            System.out.println(i);
            sum+=i;
        }
        System.out.println(sum);
    }
    public static void test7(){
        System.out.println("Pleasse Enter max:");
        Scanner scanner=new Scanner(System.in);
        int max=scanner.nextInt();
        int count=0;
        for (int i=1;i<=max;i++){
            if (i%7==0){
                System.out.println(i);
                count++;
            }
        }
        System.out.println("7的倍数的个数:"+count);
    }
    public static void test8(){
        for (int i=1;i<=3;i++){
            for (int j=1;j<=4;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
    public static void test9(){
        for (int i=1;i<=5;i++){
            for (int j=1;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
    public static void test10(){
        int type=3;
        switch(type){
            case 1:
                System.out.println("1");
                break;
            case 2:
                System.out.println("2");
                break;
            case 3:
                System.out.println("3");
                break;
            case 4:
                System.out.println("4");
                break;
            default:
                System.out.println("default"+type);
                break;
        }
    }
    public static void test11(){
        System.out.println("Please Enter month:");
        Scanner scanner=new Scanner(System.in);
        int month=scanner.nextInt();
//        if (month<0||month>12){
//            System.out.println("输入月份非法!");
//        }
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                System.out.println("31days");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.println("30days");
                break;
            case 2:
                System.out.println("Please Enter year:");
                Scanner scanner1=new Scanner(System.in);
                int year=scanner1.nextInt();
                if (year%4==0&&year%100!=0||year%400==0){
                    System.out.println("29days");
                }else{
                    System.out.println("28days");
                }
                break;
            default:
                System.out.println("输入月份非法!");
                break;
        }
    }
    public static void test12(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please Enter score:");
        int score=scanner.nextInt();
        switch (score/10){
            case 9:
            case 10:
                System.out.println("A");
                break;
            case 8:
                System.out.println("B");
                break;
            case 7:
                System.out.println("C");
            case 6:
                System.out.println("D");
                break;
            default:
                System.out.println("Not pass!");
                break;
        }
    }
    public static void test13(){
        for (int i=1;i<=5;i++){
            if(i==3){
                continue;
            }
            System.out.println(i);
        }
    }
    public static void test14(){
        for (int i=1;i<=5;i++){
            for (int j=1;j<=i;j++){
                System.out.print(j);
            }
            System.out.println();
        }
    }
    public static void test15() {
        for (int i = 1; i <= 6; i++) {
            for (int k = 1; k <= (6 - i); k++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= 2 * i - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
    public static void test16(){
        for (int i=6;i>=1;i--){
            for(int k=1;k<=(6-i);k++){
                System.out.print(" ");
            }
            for (int j=1;j<=2*i-1;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
    public static void test17(){
        int odd=0;
        int even=0;
        for (int i=1;i<=100;i++){
            if (i%2==0){
                even+=i;
            }else{
                odd+=i;
            }
        }
        System.out.println("odd:"+odd);
        System.out.println("even:"+even);
    }
    public static void test18(){
        int count=0;
        for (int i=1;i<=100;i++){
            if(i%5==0){
                System.out.print(i+"\t");
                count++;
                if (count % 3==0){
                    System.out.println();
                }
            }
        }
    }
    public static void test19(){
        int sum=1;
        for (int i=9;i>=1;i--){
            sum*=i;
        }
        System.out.println(sum);
    }
    public static void test20(){
        System.out.println("Please Enter profit:");
        Scanner scanner=new Scanner(System.in);
        double profit=scanner.nextDouble();
        double gift=0;
        if (profit<=10){
            gift=profit*0.01;
        }else if(profit>10&&profit<=20){
            gift=10*0.01+(profit-10)*0.075;
        }else if(profit>20&&profit<=40){
            gift=10*0.01+10*0.075+(profit-20)*0.05;
        }else if(profit>40&&profit<=60){
            gift=10*0.01+10*0.075+20*0.05+(profit-40)*0.03;
        }else if(profit>60&&profit<=100){
            gift=10*0.01+10*0.075+20*0.05+(profit-60)*0.015;
        }else if(profit>100){
            gift=10*0.01+10*0.075+20*0.05+40*0.015+(profit-100)*0.01;
        }
        System.out.println("gift:"+gift);
        }
    public static void test21(){
        for (int i=100;i<=200;i++){
            boolean issushu=true;
            for (int j=2;j<i;j++) {
                if (i % j == 0) {
                    issushu = false;
                    break;
                }
            }
            if (issushu==true){
                System.out.println(i+" is sushu.");
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值