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 循环结束!");// idea快捷写法 100.for 回车// 死循环
// for ( ; ; ) {
//
// }}
}
练习1: 计算1~100 之间的 奇数和偶数 的和
public class ForDemo02 {public static void main(String[] args) {// 练习1: 计算1~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);}
}
练习2: 输出1-1000之间能被5整除的数,并且每行输出3个
public class ForDemo03 {public static void main(String[] args) {//练习2: 输出1-1000之间能被5整除的数,并且每行输出3个for (int i = 1; i <= 1000; i++) {if (i%5==0){System.out.print(i+"\t");}if (i%(5*3)==0){ // 换行System.out.println();
// System.out.print("\n");}}// println 输出完成会换行// print 输出完成不会换行}
}
练习3:九九乘法表
public class ForDemo04 {public static void main(String[] args) {for (int i = 1; i <= 9; i++) {for (int i1 = 1; i1 <= i; i1++) {System.out.print(i+"*"+i1+"="+(i*i1)+"\t");}System.out.println();}}
}
https://www.bilibili.com/video/BV12J41137hu?p=40&spm_id_from=pageDriver
https://www.bilibili.com/video/BV12J41137hu?p=41&spm_id_from=pageDriver