023 for循环详解

什么是for循环

023 for循环详解_第1张图片

// 练习1
int odd = 0;
int even = 0;
for (int i = 0; i <= 100; i++) {
    if (i % 2 == 0) {
        even += i;
    } else {
        odd += i;
    }
}
System.out.println("奇数和为:" + odd + ",偶数和为:" + even);

// 练习2
for (int i = 1; i < 1000; i++) {
    if (i % 5 == 0) {
        System.out.print(i + " ");
        if (i % 15 == 0) {
            System.out.println();
        }
    }
}

// 练习3
for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= i; j++) {
        System.out.print("\t" + j + "×" + i + "=" + i * j);
    }
    System.out.println();
}

你可能感兴趣的:(JavaSE,java,算法,开发语言)