Java菜鸟入坑-循环

 

import java.util.Scanner;

public class test8 {
    public static void main(String[] args) {
        // 随机2个5-10的数字 输出较大的那一个
        int x = (int) (Math.random() * 6 + 5);
        int y = (int) (Math.random() * 6 + 5);
        if (x > y) {
            System.out.println(x);
        }else
            System.out.println(y);
        // 1-7 输出周几 8-10则输出别的
        int n = (int) (Math.random() * 10 + 1);
        //System.out.println(n);
        switch (n) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        case 3:
            System.out.println("Wednesday");
            break;
        case 4:
            System.out.println("4");
            break;
        case 5:
            System.out.println("5");
            break;
        case 6:
            System.out.println("6");
            break;
        case 7:
            System.out.println("7");
            break;
        default:
                System.out.println("I hope my weekend!");
        }
        //1-10求和
        int sum=0;
        int i=1;
        while(i<11) {
            sum+=i;
            i++;
        }
        System.out.println(sum);
        
        //1-100所有奇数相加
        int jihe=0;
        int j=1;
        while(j<100) {
            if(j%2==1) {
                jihe+=j;
                j++;
            }
            else {
                j++;
            }
        }
        System.out.println("jihe"+"="+jihe);
        //1-100内的质数求和
        int k;
        int re = 0;
        for(int i1=2;i1<101;i1++) {
            k=2;
            while(i1%k!=0)
                k++;
            if(i1==k)
                //System.out.println(i1);
                re+=i1;
        }
        System.out.println(re);
        //1-10求和(do-while)
        int x1=1;
        int sum1=0;
        do {
            sum1+=x1;
            x1++;
        }while(x1<11);
        System.out.println(sum1);
        //99乘法表
        for(int i2=1;i2<10;i2++) {
            for(int i3=1;i3<=i2;i3++) {
                System.out.print(i3+"*"+i2+"="+i2*i3+"\t");
            }
            System.out.println();
        }
        //break label语句:可以出现在任何语句中
        //break:仅出现在 while dowhile for switch 中
        /*
         * outer:for(int u=0;u<10;u++) { System.out.println("Outer loop");
         * inner:while(true) { Scanner sc=new Scanner(System.in); String s=sc.next();
         * System.out.println("inner Loop:"+s); if(s.equals("hello")) break inner;
         * if(s.equals("Kitty")) break outer; } }
         */
        //continue只能出现在while,do...while,for,if中
        //显示50以内的奇数
        for(int i4=1;i4<51;i4++) {
            if(i4%2==0) {
                continue;
            }
            System.out.print(i4+" ");
        }
        
    }
}
运行结果:

9
7
55
jihe=2500
1060
55
1*1=1    
1*2=2    2*2=4    
1*3=3    2*3=6    3*3=9    
1*4=4    2*4=8    3*4=12    4*4=16    
1*5=5    2*5=10    3*5=15    4*5=20    5*5=25    
1*6=6    2*6=12    3*6=18    4*6=24    5*6=30    6*6=36    
1*7=7    2*7=14    3*7=21    4*7=28    5*7=35    6*7=42    7*7=49    
1*8=8    2*8=16    3*8=24    4*8=32    5*8=40    6*8=48    7*8=56    8*8=64    
1*9=9    2*9=18    3*9=27    4*9=36    5*9=45    6*9=54    7*9=63    8*9=72    9*9=81    
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值