输出1-100内前5个能被3整除的数

public class BeDivided {
    
    public void m1(){
        int count = 0;
        for(int i=1;i<=100&&count<5;i++){
            if(i%3 == 0){
                count++;
                System.out.print(i+" ");
            }
        }    
    }
    
    public void m2(){
        int count = 0;
        int i = 1;
        while(i <= 100){
            if(i%3 == 0){
                count++;
                System.out.print(i+" ");
            }
            if(count == 5){
                break;
            }
            i++;
        }
    }
    
    public static void main(String[] args){
        BeDivided b = new BeDivided();
        b.m1();
        System.out.println();
        b.m2();
    }
    
}

猜你喜欢

转载自www.cnblogs.com/yxfyg/p/12332368.html
今日推荐