package com.sf.struct; public class For01 { //for 是支持迭代的一种通用结构。最有效,最灵活 //for 的循环次数在执行权确定 public static void main(String[] args) { int a = 1;//初始化条件 while(a<=100){//条件判断 System.out.println(a);//循环体 a += 2;//迭代 } for(int i=1;i<=100;i++){ System.out.println(i); } } }
package com.sf.struct; public class For02 { //求1-100内奇数和偶数的和 public static void main(String[] args) { //oddSum 奇数的和 int oddSum = 0; //evenSum 偶数的和 int evenSum = 0; for (int i = 1; i < 100; i++) { if (i%2!=0){ oddSum += i; }else{ evenSum += i; } } System.out.println(oddSum); System.out.println(evenSum); } }
package com.sf.struct; public class For03 { //输出1-1000内能被5整除的数,并每输出3个就换一次行 public static void main(String[] args) { for (int i = 1; i <= 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); // \t 制表 } if (i%(5*3)==0){ //每输出三次换行相当于每次除15换一次 System.out.print("\n"); // \n换行 } } } }
package com.sf.struct; public class For04 { //99乘法表 public static void main(String[] args) { /*for (int j = 1; j <= 9; j++) { for (int i = 1; i <= j; i++) { System.out.print(i+"*"+j+"="+(j*i)+"\t"); } System.out.println(); }*/ for (int j = 9; j >=1 ; j--) { for (int i = 1; i <= j; i++) { System.out.print(i+"*"+j+"="+(j*i)+"\t"); } System.out.println(); } } }
package com.sf.struct; public class For05 { //增强for循环 多用于数组和集合 //在JDK5时引用 public static void main(String[] args) { //定义数组 int[] numbers = {10,20,30,40,50}; //遍历数组 for (int x:numbers){ System.out.println(x); } for(int i = 0;i<5;i++){ System.out.println(numbers[i]); } } }