For循环
package com.epfox.struct;
public class ForDemo01 {
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 = oddsum + i;
}else{ //偶数
evensum = evensum + i;
}
}
System.out.println("奇数的和:" + oddsum);
System.out.println("偶数的和:" + evensum);
}
}
package com.epfox.struct;
public class ForDemo02 {
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){
System.out.println("");
// System.out.print("\n");
}
}
}
}
package com.epfox.struct;
public class ForDemo03 {
public static void main(String[] args) {
//1.我们先打印第一列
//2.我们把固定的1再用一个循环包起来
//3.去掉重复的项, i<=j
//4.调整样式
for (int j = 1; j <= 9; j++) {
for (int i = 1; i <= j; i++) {
System.out.print(j+"*"+i+"="+(i*j)+"\t");
}
System.out.println();
}
}
}