For循环详解
package zaiyang.struct;
public class ForDemo01 {
public static void main(String[] args) {
int a = 1;//初始化条件
while (a <= 100) {//条件判断
System.out.println(a);//循环体
a += 2;//迭代
}
System.out.println("while循环结束");
//初始化//条件判断//迭代
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
System.out.println("for循环结束");
}
}
package zaiyang.struct;
public class ForDemo02 {
public static void main(String[] args) {
//练习1:计算0到100之间的奇数和偶数的和
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if (i%2!=0) {//奇数
oddSum+=i; //oddSum = oddSum + i;
} else {//偶数
evenSum+=i;
}
}
System.out.println("奇数的和:" +oddSum);
System.out.println("偶数的和:"+evenSum);
}
}
package zaiyang.struct;
public class ForDemo03 {
public static void main(String[] args) {
//练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
for (int i = 0; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.print(i + "\t");
}
if (i % (5 * 3) == 0) {//每行3个
System.out.println();
//System.out.println("\n");
}
}
//println 输出完会换行
//print 输出完不会换行
}
}