for(初始化;布尔表达式;更新){ //代码语句 }
package struct; public class ForDemo01 { public static void main(String[] args) { int oddSum = 0; int evenSum = 0; for (int i = 0; i <= 100; i++) { if (i%2 != 0){ oddSum+=i; }else { evenSum+=i; } } System.out.println("所有奇数和为:"+oddSum); System.out.println("所有偶数和为:"+evenSum); } }
package struct; public class ForDemo02 { public static void main(String[] args) { for (int i = 0; i <= 1000; i++) { if (i%5 == 0){ System.out.print(i+"\t"); } if (i%(5*3) ==0){ //模为5,取三次换行 System.out.println("\n"); } } //print 输出完会换行 //println 输出完不会换行 } }
package struct; public class ForDemo02 { public static void main(String[] args) {//print 输出完会换行 //println 输出完不会换行 int j = 0; do { if (j%5 == 0){ System.out.print(j+"\t"); } if (j%(5*3) ==0){ System.out.print("\n"); } j++; }while (j<=1000); } }