首页 > 其他分享 >17-for循环

17-for循环

时间:2023-02-05 21:35:44浏览次数:39  
标签:String 17 int void 循环 static main public

public class D {
    public static void main(String[] args) {

        for (int i = 0; i < 100; i++) {
            System.out.println(i);
        }
    }
}

练习1

////////////////for方法
public class Demo07 {
    public static void main(String[] args) {
        int oddsum=0;
        int evensum=0;

        for (int i = 0; i <= 100; i++) {
            if(i%2!=0){
                oddsum=oddsum+i;
            }else{
                evensum=evensum+i;
            }
        }
        System.out.println(oddsum);
        System.out.println(evensum);
    }
}
//////////////////////while方法
public class Demo09 {
    public static void main(String[] args) {
        int oddsum=0;
        int evensum=0;
        int i=0;

        while(i>=0&&i<=100){
            if(i%2!=0){
                oddsum=oddsum+i;
            }else{
                evensum=evensum+i;
            }
            i++;
        }

        System.out.println(oddsum);
        System.out.println(evensum);
    }
}

练习2

////////for方法
public class Demo08 {
    public static void main(String[] args) {
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            if(i%15==0){
                System.out.println();
            }
            }
        }
    }
}
////////while方法
public class Demo10 {
    public static void main(String[] args) {
        int i=0;

        while(i>=0&&i<=1000){
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%15==0){
                System.out.println();
            }
            i++;
        }
    }
}

练习3

public class Demo11 {
    public static void main(String[] args) {
        
        for (int j = 1; j <= 9; j++) {

            for (int i = 1; i <= j; i++) {
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}

打印三角形

public class Demo12 {
    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            for (int k = 5; k >= i; k--) {
                System.out.print(" ");
            }
            for (int k = 1; k <= i; k++) {
                System.out.print("*");
            }
            for (int k = 1; k < i; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

标签:String,17,int,void,循环,static,main,public
From: https://www.cnblogs.com/PedroPascal/p/17093968.html

相关文章