码迷,mamicode.com
首页 > 编程语言 > 详细

黑马程序员——java基础--结构

时间:2014-11-20 23:48:04      阅读:323      评论:0      收藏:0      [点我收藏+]

标签:黑马程序员   java基础   循环   

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

一:结构

1.顺序顺序

2.选择顺序

3.循环顺序

二:具体实现

1.顺序执行

class shunxuDemo{
	public static void main(String args[]){
	int a = 1, b = 2;
	int sum = 0;
	sum = a + b;
	System.out.println("sum ="+sum);
	}
}
所有语句按照从上到下的顺序执行,没有执行判断语句。

2.选择结构

选择结构涉及几种语句:

①:if

②:if...else

③:if...else  if...else if..........else

if语句:

class shunxuDemo{
	public static void main(String args[]){
	int x = 2;
	if(x > 2){
		System.out.println("x > 2");
	}
}


if...else语句:

class shunxuDemo{
	public static void main(String args[]){
	int x = 2;
	if(x > 2){
		System.out.println("x > 2");
	}
	else{
		System.out.println("x < 2");
	}
}


if...else  if...else if..........else语句:

class shunxuDemo{
	public static void main(String args[]){
	int x = 2;
	if(x > 2){
		System.out.println("x > 2");
	}
	else if(x > 1)
	{
		System.out.println("x > 1");
	}
	else if (x > 0){
		System.out.println("x > 0");
	}
	else
		System.out.println("error");
	}
}
注意的是:if...else  if...else if..........else语句中第一个语句如果成立,后面的语句即使成立也无法执行。

当遇到某些问题,if...else  if...else if..........else语句效率就显得有些低,eg:

class shunxuDemo{
	public static void main(String args[]){
	int x = 2;
	if(x == 3 || x == 4 || x == 5){
		System.out.println("春");
	}
	else if((x ==6 || x == 7 || x == 8)
	{
		System.out.println("夏");
	}
	else if(x == 9 || x == 10 || x == 11){
		System.out.println("秋");
	}
	else if(x == 12 || x == 1 || x == 2){
		System.out.println("冬");
	}

	else
		System.out.println("error");
	}
}

这时候就要用到switch语句:

switch语句的结构:

switch(表达式)

{

case 取值1:执行语句; break;

case 取值2:执行语句; break;

....................

default :执行语句; (break;)最后一个break可以不要

}

class xuanzeDemo{
	public static void main(String args[]){
		x = 2;
		switch(x){
			case 3:
			case 4:
			case 5: 
				System.out.println("春);
				break;
			case 6:
			case 7:
			case 8:
				System.out.println("夏");
				break;
			case 9:
			case 10:
			case 11:
				System.out.println("秋");
				break;
			case 12:
			case 1:
			case 2:
				System.out.println("冬");
				break;
			default :
				System.out.println("error");
				break;

	}
}

switch中default和其他语句的执行顺序无关,默认都是先执行case语句。

switch不能使用boolean类型

if语句和switch语句的适用场合:

switch适合在数据量比较小的情况下使用,if的使用范围更广泛一点。

3.循环结构

①:while循环

②:for循环

③:do   while循环

while循环:

class WhileDemo{
	public static void main(String args[]){
		int i = 0;
	while (i < 30){
		System.out.println("x = "+x);
		i++;	
	}
}

do while循环:

class WhileDemo{
	public static void main(String args[]){
	int i = 0;
	do {
		System.out.println("x = "+x);
		i++;	
	}while(i<30);
}
for循环:

class ForDemo{
	public static void main(String args[]){
		for(int i = 0; i < 30; i++)
			System.out.println("x ="+x);
	}
}

三种循环比较:

for循环和while可以互换,但是for循环灵活性更加好一点,他们的结构不太一样,do while循环不管条件成不成立,都会执行一次。

实例:利用for循环打印矩形:

class ForDemo{
	public static void main(String args[]){
		for(int i = 0; i < 5; i++){
			for(int j = 0; j < 3; j++){
				System.out.print("*");
			}
			System.out.println();
		}
			
	}
}
打印结果:

***

***

***

***

***

得出结论:外层循环控制矩形的行数,内层循环控制每行的个数,也就是列数。

由此可以打印:

*

**

***

****

*****

class ForDemo{
	public static void main(String args[]){
		for(int i = 0; i < 5; i++){
			for(int j = 0; j <= i; j++){
				System.out.print("*");
			}
			System.out.println();
		}
			
	}
}


如果打印:

*****

****

***

**

*

class ForDemo{
	public static void main(String args[]){
		for(int i = 0; i < 5; i++){
			for(int j = i; j < 5; j++){
				System.out.print("*");
			}
			System.out.println();
		}
			
	}
}

得出一个结论:

尖朝上,可以改变条件,让条件随着外循环变化

尖朝下,可以改变初始值,让条件随着外循环变化

由此可以打印九九乘法表:


class ForDemo{
	public static void main(String args[]){
		for(int i = 0; i < 9; i++){
			for(int j = i; j <= i; j++){
				System.out.print(i+"*"+"j+"="+i*j+"\t");
			}
			System.out.println();
		}
			
	}
}

最后就可以打印:

      *

     * *

   *  *  *

  *  *  *  *

*  *  *   *   *    

这个可以看做:

----*

---* * 

--* * *

-* * * * 

* * * * *

先打印左边的倒三角,然后在打印右边的正三角:

class ForDemo{
	public static void main(String args[]){
		for(int i = 0; i < 5; i++){
			for(int j = i; j < 5; j++){
				System.out.print(" ");
			}
			for(int j = 0; j <= i; j++){
				System.out.print("*");
			}
			System.out.println();
		}
			
	}
}

到底就差不多了,这一节视频基本上就可以这样总结结束,有什么不同的想法还望大家多多指教。


黑马程序员——java基础--结构

标签:黑马程序员   java基础   循环   

原文地址:http://blog.csdn.net/orangeisnotapple/article/details/41321681

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!