输出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();
    }
    
}

 

posted @ 2020-02-19 18:02  yxfyg  阅读(638)  评论(0)    收藏  举报