您的位置:首页 > 职场人生

黑马程序员_Java开发基础_02_流程控制

2014-05-26 22:57 323 查看
----------Android、Java培训期待与您交流----------
Java 流程控制
一.顺序结构

   顺序结构是最简单的程序结构,也是最常用的程序结构,只要按照解决问题的顺序写出相应的语句就行,它的执行顺序是从上到下,依次执行

二.选择结构

   选择结构程序用于判断给定的条件,根据判断结果执行相应的语句来控制流程。

   1.if(boolean exp){……}

     如果布尔表达式结果为真,则执行循环体的的语句,否则顺序执行循环体后面的语句

   2.if(boolean exp){……}else{……}

     如果如果布尔表达式结果为真,则执行循环体的的语句;否则执行else循环体内的语句,执行完毕继续顺序执行循环体外的语句

   3.if(boolean exp){……}else if(boolean){……}

      如果布尔表达式结果为假,则判断所属下一个else if表达式的值,如果为真执行循环体语句,剩下的条件判断体不再执行,否则继续判断之后的布尔表达式,直到所有布尔表达式判断完毕

   4.switch(byte\char\int\short){case a: ……;case b:……;}

     switch所接受的变量的类型:byte、char、int、short(jdk1.6之前 ),块内先判断case a,如果和switch的值相同则执行相应语句,否则继续向下判断

   5.三目/条件运算符:boolean exp a?exp m:exp n;

      如果表达式a结果为真,则执行表达式m,否则执行表达式n

三.循环结构

   1.while(boolean exp){……}

     如果表达式结果为真,则执行一次循环体内的语句,执行完毕再判断布尔表达式的值,为真则执行循环体语句,为假则不执行循环体语句,循环结束

   2.do{……}while(boolean exp);

     先执行依次do后面的语句,接下来判断布尔表达式,如果为真则继续执行依次do后面的循环语句,否则循环结束

   3.for(exp list a;exp list b;exp list c){……}

     先执行表达式列表a,下来执行表达式列表b,下来执行循环体,循环体执行完毕再执行表达式列表c,再下来执行表达式列表b;如果条件符合执行循环体语句,否则循环结束

     

  终止循环: break和continue用法

  break:终止当前循环,跳出本层循环
  continue:终止当次循环,继续判断循环条件、执行循环体

public class ProControl {
static boolean isCloudy;

public static void main(String[] args) {
double score = 85;

isCloudy = true;
// 选择结构
// if(boolean exp){……}
if (isCloudy == false) {
System.out.println("go cinema to see a movie");
}
// if(boolean exp){……}else{……}
if (isCloudy == true) {
System.out.println("stay home and see a movie");
} else {
System.out.println("go cinema to see a movie");
}
// if(boolean exp){……}else if(boolean exp){……}……{……}
if (score > 90) {
System.out.println("A");
} else if (score > 80) {
System.out.println("B");
} else if (score > 70) {
System.out.println("C");
}

// score=Math.round(100*Math.random());
// switch
switch (7) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
default:
System.out.println("Date is Wrong");
break;
}
// 循环结构
// while(boolean exp){……}

while (isCloudy) {
System.out.println("stay home and see a movie");
isCloudy = false;
}
System.out.println("go cinema to see a movie");
boolean isHavaCash = true;
// do{……} while(boolean exp);
do {
System.out.println("use cash!");
isHavaCash = false;
System.out.println("-------------------");
System.out.println("you are running out of cash");
} while (isHavaCash == true);
System.out.println("now begin to use credit card");

// for循环
// for(exp list;exp list;exp list){……}
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("sum=" + sum);
// 循环终止关键字break和continue
// break
for (int i = 0; i < 10; i++) {
System.out.print(i + "\t");
if (i >= 5) {
break;
}
}
System.out.println();
// continue
for (int i = 0; i < 10; i++) {
System.out.print(i+"\t");
if (i >= 5) {
continue;
}
}
System.out.println();
// print triangle
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("------------");
for (int i = 1; i <= 5; i++) {
for (int j = i; j <= 5; j++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("-----------------");
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print("*");
}
System.out.println();
}
System.out.println("-----------------");
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= i; j--) {
System.out.print(" ");
}
for (int z = 1; z <= i; z++) {
System.out.print("* ");
}
System.out.println();

}
System.out.println("---------------");
// print multiplication table
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + j * i + "\t");
}
System.out.println();
}
}
}


[align=center]----------Android、Java培训期待与您交流----------[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  黑马程序员 java