Java程序逻辑控制
- 七、Java程序逻辑控制
- 7.1、IF 分支结构
- 观察if语句
- 使用if…else
- 多条件判断
- 7.2、SWITCH开关语句
- 观察switch语句
- 使用break语句
- 判断字符串
- 7.3、while循环
- 实现1~100的累加
- 使用do…while实现数组累加
- while循环和do…while循环的最大差别:
- 7.4、for循环
- 使用for循环实现1~100的累加
- 对于while和for循环的选择标准:
- 7.5、循环控制
- break退出整个循环结构
- continue只是结束当前的一次调用
- 7.6、嵌套循环
- 打印乘法口诀表
- 打印三角形
七、Java程序逻辑控制
在程序开发的过程之中一共会存在有三种程序逻辑:顺序结构、分支结构、循环结构,对于之前编写的代码大部分都是顺序结构的定义,即:所有的程序将按照定义的代码顺序依次执行。
7.1、IF 分支结构
If分支结构主要是针对于关系表达式进行判断处理的分支操作。对于分支语句主要有三类的使用形式,使用的关键字:if、else。
语法 | 描述 |
if | 满足时执行一次 |
if else | 如果 否则 |
if else if else | 可以多检测条件判断 |
if判断:
if(布尔表达式){
条件满足时执行;
}
if…else…判断:
if(布尔表达式){
条件满足时执行;
}else{
条件不满足时执行;
}
多条件判断:
if (布尔表达式) {
条件满足时执行 ;
} else if (布尔表达式) {
条件满足时执行 ;
} else if (布尔表达式) {
条件满足时执行 ;
} [else {
条件不满足时执行 ;
}]
观察if语句
public class test {
public static void main(String [] args){
int age = 18;
if(age >= 18 && age <= 20){
System.out.println("听说点赞的人都很好看!!");
}
System.out.println("谢谢.");
}
}
Output:
听说点赞的人都很好看!!
谢谢.
如果说现在要进行不满足条件时的执行可以使用if…else操作。
使用if…else
public class test {
public static void main(String [] args){
int money = 19;
if(money >= 19.8){
System.out.println("来一份巨无霸汉堡!");
}else{
System.out.println("来一份吉士汉堡");
}
System.out.println("味道不错!");
}
}
Output:
来一份吉士汉堡
味道不错!
在使用if的时候最主要的特点是它可以进行若干个条件判断。
多条件判断
public class test {
public static void main(String args[]) {
double score = 90.00 ;
if (score >= 90.00 && score <=100) {
System.out.println("优秀") ;
} else if(score >=80 && score < 90) {
System.out.println("良好") ;
} else if(score >=60 && score < 80) {
System.out.println("及格") ;
} else {
System.out.println("不及格") ;
}
}
}
Output:
优秀
在进行多条件判断的时候可以不写上else语句,但是好的习惯一定要写上else语句。
7.2、SWITCH开关语句
Switch是一个开关语句,它主要是根据内容来进行的判断,需要注意的是switch中可以判断的只能够是数据(int、char、枚举、String),而不能够使用逻辑判断,它的语法如下:
switch(数据) {
case 数值 : {
数值满足时执行 ;
[break ;]
}
case 数值 :
数值满足时执行 ;
[break ;]
[default:
所有判断数值不满足时的执行 ;
[break ;]
]
}
case后可不加{},但是建议加
观察switch语句
public class test {
public static void main(String [] args){
int ch = 2;
switch (ch){
case 2:
System.out.println("设置的内容是2");
case 1:
System.out.println("设置的内容是1");
default:
System.out.println("没有内容满足.");
}
}
}
Output:
设置的内容是2
设置的内容是1
没有内容满足.
Switch语句在进行设计的时候,如果你在每一个case后面没有追加break语句,那么会在第一个匹配的case之后继续执行,一直到全部的switch中后续代码执行完毕或者遇见break。
使用break语句
public class test {
public static void main(String [] args){
int ch = 1;
switch (ch){
case 2:
System.out.println("设置的内容是2");
break;
case 1:
System.out.println("设置的内容是1");
break;
default:
System.out.println("没有内容满足.");
break;
}
}
}
Output:
设置的内容是1
从JDK1.7的时代开始,Oralce公司推出的JDK1.7版本里面将开发者呼吁10年以上的功能加入到了系统之中,可以进行字符串数据的判断。
判断字符串
public class test {
public static void main(String [] args){
String str = "hello";
switch (str){
case "Hello":
System.out.println("Hello");
break;
case "hello":
System.out.println("hello");
break;
default:
System.out.println("NoMatch");
break;
}
}
}
Output:
hello
Switch这个语句是一个编程语言发展的重要技术。
7.3、while循环
所谓的循环结构指的是某一段代码被重复执行的处理操作,在程序之中提供有while语句来实现循环的定义,该语句有两类,该语句有两类使用形式:
while循环:
while(布尔表达式){
条件满足时执行;
修改循环条件;
}
do…while 循环:
do {
条件满足时执行;
修改循环条件;
} while(布尔表达式);
实现1~100的累加
public class test {
public static void main(String [] args){
int count = 0; //保存最终的计算总和
int x = 1; //进行循环控制
while(x <= 100) { //循环的执行条件
count += x; //累加
x++; //修改循环条件
}
System.out.println(count);
}
}
Output:
5050
除了使用while循环之外也可以使用do…while来进行处理。
使用do…while实现数组累加
public class test {
public static void main(String [] args){
int count = 0; //保存最终的计算总和
int x = 1; //进行循环控制
do{ //循环的执行条件
count += x; //累加
x++; //修改循环条件
}while(x<=100);
System.out.println(count);
}
}
Output:
5050
while循环和do…while循环的最大差别:
while循环是先判断后执行,而do…while先执行一次后判断。
开发中能见到do…while循环的几率几乎为1%左右,99%的情况下首选的肯定是while循环。
7.4、for循环
For循环也是一种常规的使用结构,其使用语法如下:
for (定义循环的初始化数值 ; 循环判断 ; 修改循环数据) {
循环语句的执行 ;
}
使用for循环实现1~100的累加
public class test {
public static void main(String args[]) {
int sum = 0 ; // 保存最终的计算总和
for (int x = 1 ; x <= 100 ; x ++) {
sum += x ; // 累加
}
System.out.println(sum) ;
}
}
Output:
5050
如果现在要想更加清楚的观察到三个操作定义,可以拆开处理。
public class test {
public static void main(String args[]) {
int sum = 0 ; // 保存最终的计算总和
int x = 1 ; // 循环条件初始化
for ( ; x <= 100 ; ) {
sum += x ; // 累加
x ++ ; // 修改循环条件
}
System.out.println(sum) ;
}
}
Output:
5050
对于while和for循环的选择标准:
- 明确循环次数情况下for优先
- 在不知道循环次数,但知道循环结束条件时使用while循环
7.5、循环控制
在循环语句定义的时候还有两个控制语句:break、continue;
break退出整个循环结构
public class test {
public static void main(String [] args){
for(int x = 0; x <= 10; x++){
if(x > 3){
break;
}
System.out.print(x + "、");
}
}
}
Output:
0、1、2、3、
continue只是结束当前的一次调用
public class test {
public static void main(String [] args){
for(int x = 0; x <= 10; x++){
if(x == 3){
continue;
}
System.out.print(x + "、");
}
}
}
Output:
0、1、2、4、5、6、7、8、9、10、
当执行到了continue的时候就表示在当前的语句之中后续代码不再执行,而直接进行后续的判断处理。
在C语言里面有一个goto的指令,这个指令会直接造成代码的混乱,所以在开发之中一般都对其深恶痛绝,但是在Java可以利用continue实现部分goto的功能。
public class test {
public static void main(String [] args){
point: for(int x = 0; x <= 10; x++){
for(int y = 0; y<3;y++){
if(x == y){
continue point;
}
System.out.print(x + "、");
}
System.out.println();
}
}
}
Output:
1、2、2、3、3、3、
4、4、4、
5、5、5、
6、6、6、
7、7、7、
8、8、8、
9、9、9、
10、10、10、
对于此类代码强烈不建议开发者在开发代码中出现。
7.6、嵌套循环
在一个循环语句之中嵌套其它的循环语句就称为循环嵌套处理,循环嵌套层次越多时间复杂度就越高,那么下面通过循环嵌套观察两个简单的程序。
打印乘法口诀表
public class test {
public static void main(String [] args){
for(int x = 1; x<=9; x++){
for(int y = 1;y<=x;y++){
System.out.print((y+"*"+x+"=")+y*x + "\t");
}
System.out.println();
}
}
}
Output:
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
打印三角形
public class test {
public static void main(String [] args){
int line = 5;
for(int x = 1; x<=line; x++){
for(int y = 1;y<=line-x;y++){
System.out.print(" ");
}
for(int z = 1;z<=x;z++){
System.out.print("* ");
}
System.out.println();
}
}
}
Output:
*
* *
* * *
* * * *
* * * * *