衣带渐宽终不悔。为伊消得人憔悴。
——柳永《蝶恋花》
[TOC]
流程控制语句
1. 顺序结构
顺序结构(Sequence structure)的程序设计是最简单的,只要按照解决问题的顺序写出相应的语句就行,它的执行顺序是自上而下,依次执行。
2. 分支(选择)结构
2.1 If
1 2 3 4 5 6 7 8 9 10 11 12 13
   | boolean miao=true; if(miao){ 	System.out.println("哎呦~喵喵喵.."); }else{ 	System.out.println("我们一起学猫叫"); }
 
  if(!miao){ 	System.out.println("我们一起学猫叫"); }else{ 	System.out.println("哎呦~喵喵喵.."); }
   | 
 
2.2 If…Else
if和else条件是互斥的,意思是:两者的条件只能有一个成立(不能同时成立);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | public static void main(String[] args) {      		int age=17;      		if(age>=18){ 			System.out.println("喵喵可以交楠盆友了!"); 		}else{ 			System.out.println("喵喵今年十七还是个孩纸。"); 		}      		 		if(age%2==0){ 			System.out.println(age+"nianling是偶数"); 		}else{ 			System.out.println(age+"nianling是奇数");                     }
  | 
 
2.3 If…Else if
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
   | public static void main(String[] args)  {         	System.out.println("学生成绩等级");
  	double scores=79.5; 	if(scores>=90){ 		System.out.println("A等级"); 	}else if(scores>=80){ 		System.out.println("B等级"); 	}else if(scores>=70){ 		System.out.println("C等级"); 	}else if(scores>=60){ 		System.out.println("D等级"); 	}else{ 		System.out.println("F等级"); 	}           }
   | 
 
2.4 switch
switch中的数据类型:
- 基本数据类型:byte/short/int/char
 
- 引用数据类型:String(JDK1.7才引进的String类型)/enum
 
switch语句非常灵活
- case语句的顺序可以颠倒,break语句记得加上。
 
匹配到哪一个case,就从哪一个case开始执行,直到遇到break语句才会结束。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   | public static void main(String[] args)  {
  	int num=1;         	switch(num){ 		case 1: 			System.out.println("星期一"); 		 		break;                 		case 2: 			System.out.println("星期二"); 			break; 		case 3: 			System.out.println("星期三"); 			break; 		case 4: 			System.out.println("星期四"); 		    break; 		case 5: 			System.out.println("星期五"); 			break; 		case 6: 			System.out.println("星期六"); 			break;	 		case 7: 			System.out.println("星期日"); 			break; 	}
  	System.out.println("喵喵喵~"); }
   | 
 
3. 循环结构
3.1 For
循环结构:将重复的工作放在一起去做。
循环结构:四部分(以及执行顺序)
- 初始化语句:  循环开始的时候执行,只执行一次。
 
- 条件判断:      如果成立,循环继续,如果不成立,循环退出。
 
- 循环体:          重复的工作。
 
- 步进:              每次循环之后,都要去进行的工作。
 
1 2 3 4 5 6 7 8
   |  public static void main(String[] args)  { 	for (int i=0;i<522 ;i++ ) 	{ 		System.out.println("哎呦~喵喵喵..."+(i+1)); 	} }
 
  | 
 
3.2 while 和 do…while
实现:1+2+3+4+…+100;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   | 
 
 
 
 
 
 
 
  		int i=0; 		while(i<100){ 			sum+=(i+1); 			i++; 		} 		System.out.println(sum);
 
 
 
 
 
 
 
 
 
 
  		int j=0; 		do{ 			sum+=(j+1); 			j++; 		}while(j<100); 		System.out.println(sum);
 
  | 
 
知道循环次数的时候使用for
不知道循环的具体次数的时候使用while
3.3 break 和 continue
break语句:打断循环,一旦执行了break语句,当前循环就结束了。
利用break;可以控制在循环次数内结束循环。
continue : 另外控制循环语句的关键字continue一旦执行了,当次就结束了,立马进入到下一次的循环。
3.4 循环结构练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
   | 
 
 
  	public static void main(String[] args)  	{ 		 		for (int i=0; i<23; i++) 		{ 			 			for (int j=1;j<=60 ;j++ ) 			{ 				System.out.println(i+"时"+j+"分钟"); 			} 		} 	}
 
 
  	public static void main(String[] args)  	{ 		 		double height=100; 		 		double s=0;
  		for (int i=0;i<10;i++) 		{ 			 			s +=(height*2); 			 			height /=2; 		} 		s -=100;
  		System.out.println(s); 		System.out.println(height); 	}
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
   |  			 A		 		   *B*       		  **C**		 		 ***D***	 		****E****	 	   *****F*****   		****G****	 		 ***H***	 		  **I**		 		   *J*		 			 K		
 
  	public static void main(String[] args)  	{ 		int m=11; 		char ch = 'A'; 		ch -= 1;
  		for (int i=1;i<=m ;i++ ){ 			int num1=2*i-1; 			int num2=m/2+1-i; 			int zm=i;
  			if(i>m/2){ 				num1=2*(m+1-i)-1; 				num2=i-(m/2+1); 				zm=m+1-i; 			}
  			for (int k=1;k<=num2 ;k++ ){ 				System.out.print(" "); 			}
  			for (int j=1;j<=num1 ;j++ ){ 				if(j==zm){ 					System.out.print(ch +=1); 				}else{ 					System.out.print("*"); 				} 			} 			System.out.println(" ");
  		} 		
  	 	}
 
  |