java流程控制语句的总结
流程控制语句,顾名思义,就是控制程序走向的语句,其中包括条件语句,分支语句和循环语句。
一、条件语句:if条件句有三种形式
1:if(表达式){方法体}
2:if(表达式){方法体} else {方法体}
3:if(表达式){方法体} else if(表达式){方法体} else{方法体}
表达式的结果是一个布尔值,如果是true,直接进入if的方法体中,如果结果为false,则跳过if的方法体,继续执行。
示例:
二、分支语句:分支语句和if条件句的意思大同小异,但是当判断的条件非常多时,用switch分支语句就会简单的多,而且看起来比较简洁。这里需要注意的是:1,switch语句中不能忘记break,否则会有贯穿现象。2,switch的应用有所限制,其表达式只能应用于byte,short,int,char新版本的JDK也可以支持String类型3,if else适合范围的选择,而switch只能是固定值的选择Switch语句的表现形式:switch(expression){ case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of casestatements. default : //Optional //Statements } 上面的示例程序的档位选择,可以改变从下面的样子 //档位选择判断
switch (input) {
case 1:
System.out.print("你选择的是电压档位");
break;
case 2:
System.out.print("你选择的是电流档位");
break;
case 3:
System.out.print("你选择的是电阻档位");
break;
case 4:
System.out.print("你选择的是其他档位");
break;
default:
System.out.print("你输入的档位有误! ");
break;
}
三、循环语句:包括while循环语句,do…while循环语句和for循环
其中while(布尔表达式)和 do…while(布尔表达式)类似,while是先判断后执行,do…while是先执行一次然后再判断条件。如果布尔表达式结果为真,那么两个循环语句结果相同,若布尔表达式的第一次结果为假,do…while会先执行一次,而while则不会继续执行。
while语法表现形式:
while(Boolean_expression)
{
//Statements
}
do while语法表现形式:
do{
//Statements
} while(Boolean_expression)
下面的程序和示例1差不多,但是有一个while循环,如果输入档位不对,那么会一直循环
public class Multimerter {
/*
* if else的使用
* 万用表的设计
* 1电压 2电流 3电阻 4其他档位
* 除此之外提示用户输入正确的选择
* */
public static void main(String[] args) {
System.out.println(" 欢迎您使用万用表");
System.out.println("1电压 2电流 3电阻 4其他档位");
System.out.print("请输入您要选择的档位:");
Scanner scanner=new Scanner(System.in);
int input =scanner.nextInt();//获取输入的档位
while (!(input==1 ||input==2 ||input==3 ||input==4)) {//确保输入的数值只能是1、2、3、4
System.out.print("你输入的档位不正确!请重新输入:");
input =scanner.nextInt();//获取输入的档位
}
//档位选择判断
if (input==1) {
System.out.print("你选择的是电压档位");
}else if (input==2) {
System.out.print("你选择的是电流档位");
}else if (input==3) {
System.out.print("你选择的是电阻档位");
} else {
System.out.print("你选择的是其他档位");
}
scanner.close();//关闭输入流
}
}
While的标准示例:
public classTest {
public staticvoid main(Stringargs[]) {
int x= 10;
while(x < 20 ) {
System.out.print("valueof x : " + x );
x++;
System.out.print("\n");
}
}
}
输出结果:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
for循环格式:for(变量初始化;条件判断;步进){循环语句},只进行一步初始化,然后进行条件判断,为真后执行for中的循环语句,最后执行迭代语句(如i++),然后继续进行条件判断,直到结果为假,跳出循环。
For循环语句的表现形式
for(initialization;Boolean_expression; update)
{ //Statements
}
For的标准示例
输出结果:和上面的while示例的结果是一样的
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
For循环在输出数组有一个很方便的方式:
for括号里面要定义一个和数组值一样的类型变量,for循环会每次赋值数组里面的一个数给该变量
这样输出是很方便的
输出结果:10,20,30,40,50,James,Larry,Tom,Lacy, 循环语句的中断和继续,break&continuebreak:用于跳出整个循环。continue:用于跳出当前循环,开始执行下一次循环的执行过程。 使用Continue的示例:
public static void main(String args[]) { continue; System.out.print(x ); System.out.print("\n"); } }
这将产生以下结果:10 20 40 50
使用break的示例:
public class Test { for(int x : numbers ) { System.out.print(x ); }
输出结果:1020
应用题:
结果如下:
思考题:
for(inti=0;i<6;i++){
for(int j=i;j<10;j++){
while(j==4){
j++;
continue;
}
if(j>1){
break;
}
System.out.println(“i=”+i+“,j=“+j);
}
}
输出:?