While循环详解
1 package struct; 2 3 public class WhileDemo01 { 4 public static void main(String[] args) { 5 int i = 0; 6 while(i<100){ 7 i++; 8 System.out.println(i); 9 } 10 } 11 }
1 package struct; 2 3 public class WhileDemo02 { 4 public static void main(String[] args) { 5 //死循环 6 while(true){ 7 //等待客户端连接 8 //等时检查 9 // 10 } 11 } 12 }
1 package struct; 2 3 public class WhileDemo03 { 4 public static void main(String[] args) { 5 //计算1+2+3+...+100=? 6 int i = 0 ; 7 int sum = 0; 8 9 while(i<=100){ 10 sum = sum +i; 11 i++; 12 } 13 System.out.println(sum);//5050 14 } 15 }
1 package struct; 2 3 public class DoWhileDemo01 { 4 public static void main(String[] args) { 5 int i = 0; 6 int sum = 0; 7 do { 8 sum = sum + i; 9 i++; 10 }while (i<=100); 11 System.out.println(sum);//5050 12 } 13 }
1 package struct; 2 3 public class DoWhileDemo02 { 4 public static void main(String[] args) { 5 int a = 0; 6 while(a<0){ 7 System.out.println(a); 8 a++; 9 } 10 System.out.println("====================="); 11 do { 12 System.out.println(a); 13 a++; 14 }while (a<0); 15 } 16 }