—— 程序的运行流程控制会解决程序是如何执行的
三大程序流程控制结构
1)顺序结构:程序从上到下逐行逐行的执行,中间没有任何的判断和跳转
2)分支结构:程序具有选择性的进入某一段代码
3)循环结构:程序重复的执行某一段代码
思维导图
一.if分支结构
1.什么叫做分支结构
——程序有选择性的执行某一段代码,使程序具有选择性
2.if分支结构形式
1)单分支:if (条件表达式)
2)双分支:if (条件表达式) else
3)多分支:if (条件表达式) else if (条件表达式) else if (条件表达式) …… else
3.一个小case快速了解if分支结构
public class Test {public static void main(String[] args) {int a = 10;int b = 20;//单分子if (a > b) {System.out.println("a > b");}//双分支if (a < b) {System.out.println("a < b");//a < b} else {System.out.println("a > b");}//多分支if (a > b) {System.out.println("a > n");} else if (a < b) {System.out.println("a < b");//a < b} else {System.out.println("a == b");}}
}
4.分支语句详解
1)单分支形式:if (条件表达式) { 代码块; }
解释:条件表达式为true,则执行if语句里面的代码块,若是条件表达式为false,就跳过if语句,执行if (){ }后面的语句
import java.util.Scanner;
public class Test {public static void main(String[] args) {//单分支的实现int num1 = 0;int num2 = 0;Scanner sc = new Scanner(System.in);//输入条件//给num1,num2输入值num1 = sc.nextInt();num2 = sc.nextInt();//单分支if (num1 > num2) {System.out.println(num1 + ">" + num2);}if (num1 < num2) {System.out.println(num1 + "<" + num2);}}
}
2)双分支形式:if (条件表达式) { 代码块; } else { 代码块; }
解释:如果if的条件表达式为true,则执行if中的代码块,如果if的条件表达式为false,则执行else中的代码块
import java.util.Scanner;
public class Test {public static void main(String[] args) {//双分支的实现int num1 = 0;int num2 = 0;Scanner sc = new Scanner(System.in);//输入条件//给num1,num2输入值num1 = sc.nextInt();num2 = sc.nextInt();//双分支if (num1 > num2) {System.out.println(num1 + ">" + num2);} else {System.out.println(num1 + "<" + num2);}}
}
3)多分支形式:
if (条件表达式) { 代码块; } else if (条件表达式) else if (条件表达式) …… else { 代码块; }
解释:那个的条件表达式为true,则执行那个分支的语句块,如果都为false,则执行else中的语句块
import java.util.Scanner;
public class Test {public static void main(String[] args) {//多分支的实现int num1 = 0;int num2 = 0;int num3 = 0;Scanner sc = new Scanner(System.in);//输入条件//给num1,num2输入值num1 = sc.nextInt();num2 = sc.nextInt();num3 = sc.nextInt();//多分支if (num1 > num2) {System.out.println(num1 + ">" + num2);} else if (num1 > num3) {System.out.println(num1 + ">" + num3);} else if (num2 > num3) {System.out.println(num2 + ">" + num3);} else {System.out.println("hehe");}}
}
4)分支嵌套
在if语句里面含有if语句,再在if语句里面有if语句,……;在else语句里面含有if语句……(注意:最后不要嵌套超过3层,可读性不好)
import java.util.Scanner;
public class Test {public static void main(String[] args) {//分支嵌套的实现int data1 = 0;int data2 = 0;int data3 = 0;Scanner sc = new Scanner(System.in);//输入条件//给num1,num2输入值data1 = sc.nextInt();data2 = sc.nextInt();data3 = sc.nextInt();//分支嵌套if (data1 > data2) {if (data1 > data3) {System.out.println("最大值是:" + data1);} else {System.out.println("最大值是:" + data3);}} else {if (data2 > data3) {System.out.println("最大值是:" + data2);} else {System.out.println("最大值是:" + data3);}}}
}
二.switch分支结构
1.switch的语法规则
switch (表达式) {case 常量1:语句块;break;case 常量2:语句块;break;case 常量3:语句块;break;……default:语句块;break;//default后加上break养成好习惯
}解释:1)switch (表达式)中的表达式匹配对应的一个值,用于和case中的值匹配2)case 常量:当表达式的值等于常量,则执行case对应的语句块3)break: 表示退出switch语句4)如果表达式没有和任何一个case匹配上,则执行default语句
2.一个小case快速了解switch语句
import java.util.Scanner;
public class Test {public static void main(String[] args) {//switch的使用int today = 0;//输入Scanner sc = new Scanner(System.in);today = sc.nextInt();switch (today) {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;default:System.out.println("日期错误,请检查后重试");break;}}
}
3.switch的细节讨论
1)switch中的表达式类型应和case后面的常量类型一致,或者是可以自动转换成可以相互比较的类型
import java.util.Scanner;
public class Test {public static void main(String[] args) {//switch中的表达式类型应和case后面的常量类型一致,或者是可以自动转换成可以相互比较的类型char ch = 0;//输入Scanner sc = new Scanner(System.in);ch = sc.next().charAt(0);//获取字符串的第一个字符switch (ch) {//char类型自动转换成int类型进行比较case 97:System.out.println("a");break;case 98:System.out.println("b");break;case 99:System.out.println("c");break;case 100:System.out.println("d");break;case 101:System.out.println("e");break;default:System.out.println("其他字符:" + ch);break;}}
}
2)switch(表达式)中的值类型必须是:byte、short、int、char、enum、String类型
public class Test {public static void main(String[] args) {//switch (表达式)中表达式的返回值必须是:byte、short、int、char、enum、String类型byte b = 0;short s = 0;int num = 0;char ch = 0;String str = "abc";switch (b) {}//正确switch (s) {}//正确switch (num) {}//正确switch (ch) {}//正确switch (str) {}//正确//不能使用的类型:float、double、long等long data = 0;float f = 1.3f;double db = 1.45;//switch(f) {} //错误//switch(db) {} //错误//switch(data) {} //错误}
}
3)case 紧跟的值必须是常量,不能是变量或浮点类型
public class Test {public static void main(String[] args) {//case 紧跟的值必须是常量,不能是变量或浮点类型int num = 10;int data = 11;double f = 13.14;switch (num) {//case data: //case后面不能跟变量//case f: //case后面不能跟变量//case 13.14: //case后面不能跟浮点类型}}
}
4)default语句可省略,在所有的case语句都没有匹配成功的情况下,执行default语句
5)break语句用来执行完一个case分支后跳出switch语句块,如果没有写break语句,程序会顺序执行到switch的结尾或执行到有break的case分支语句
import java.util.Scanner;
public class Test {public static void main(String[] args) {//break语句用来执行完一个case分支后跳出switch语句块//如果没有写break语句,程序会顺序执行到switch的结尾或执行到有break的case分支语句int season = 0;//输入Scanner sc = new Scanner(System.in);season = sc.nextInt();switch (season) {case 12:case 1:case 2:System.out.println("冬季");break;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;default:System.out.println("月份有误,请检查!!!");break;}}
}
4.switch和if的比较
1)配对比较,而且符合byte、short、int、char、enum、String6种类型,建议使用switch语句
2)区间判断,结果是boolean类型的数据,使用if分支语句
三.循环结构
1.什么是循环结构
——程序中循环的执行某些代码,使用while、do-while、for进行循环
2.循环结构的格式
while(循环条件) {//循环代码块
}do {//循环代码块
}while(循环条件);for(循环初始化; 循环条件; 循环变量迭代) {//循环代码块
}
3.一个小case快速了解循环结构
public class Test {public static void main(String[] args) {//while循环int i = 1;while (i <= 10) {System.out.print(i + "\t");i++;}System.out.println();//do-while循环i = 1;do {System.out.print(i + "\t");i++;} while (i <= 10);System.out.println();//for循环for (i = 1; i <= 10; i++) {System.out.print(i + "\t");}}
}
4.循环四要素
1)循环语句
2)循环体
3)循环判断
4)循环变量迭代
public class Test {public static void main(String[] args) {//while循环int i = 1;//循环变量//循环判断while (i <= 10) {//循环体System.out.print(i + "\t");i++;//循环变量迭代}System.out.println();}
}
5.while、do-while循环的细节讨论
1)当循环条件为假(false)时,循环中的循环体不执行,循环条件为真(true)时,执行循环中的循环体
public class Test {public static void main(String[] args) {//while循环int i = 11;//循环变量//循环条件为false,不执行循环体while (i <= 10) {//循环体}//条件为truewhile (i < 15) {System.out.print(i + "\t");i++;}System.out.println();}
}
2)do-while循环先执行后判断
3)break在while、do-while中的作用:跳出循环,执行循环后面的语句
public class Test {public static void main(String[] args) {//while循环int i = 1;while (i <= 10) {if (i == 5)break;//跳出循环System.out.print(i + "\t");//1 2 3 4i++;}System.out.println();//换行i = 1;do {if (i == 5)break;//跳出循环System.out.print(i + "\t");//1 2 3 4i++;} while (i <= 10);}
}
4)continue在while、do-while中的作用:跳过本次循环,执行下一次循环
5)while循环常使用在不知循环次数的情况下
public class Test {public static void main(String[] args) {//while循环int i = 1;while (i <= 10) {if (i == 5)continue;//跳过本次循环System.out.print(i + "\t");//1 2 3 4 死循环i++;}System.out.println();//换行}
}
6.for循环的细节讨论
1)for循环的循环变量只初始化一次
2)break在for循环中的作用:跳出循环,执行循环后的语句
public class Test {public static void main(String[] args) {for (int i = 1; i < 10; i++) {if (i == 5)break;System.out.print(i + " ");// 1 2 3 4}}
}
3)continue在for循环中的作用:跳过本次循环,但是循环变量迭代依然要执行
4)for循环使用在易知道循环次数的情况下
public class Test {public static void main(String[] args) {for (int i = 1; i < 10; i++) {if (i == 5)continue;System.out.print(i + " ");// 1 2 3 4 6 7 8 9}}
}
7.三种循环的区别
1)do-while循环先使用后执行
2)三种循环都有循环四要素
3)三种循环都需要使用条件判断
4)循环表达式判断的结果都是boolean型
8.多重循环的使用
1)打印金字塔
import java.util.Scanner;
public class Test {public static void main(String[] args) {int len = 0;//输入Scanner sc = new Scanner(System.in);len = sc.nextInt();//金字塔for (int i = 0; i < len; i++) {for(int j = 0; j < len - i; j++) {System.out.print(" ");}for (int j = 0; j <= i; j++) {System.out.print('*' + " ");}System.out.println();}}
}
2)九九乘法表
public class Test {public static void main(String[] args) {//乘法表int len = 0;//输入Scanner sc = new Scanner(System.in);len = sc.nextInt();for (int i = 1; i <= len; i++) {for(int j = 1; j <= i;j++) {System.out.print(j + "*" + i + "=" + j*i + "\t");}System.out.println();}}
}
3)登录验证
import java.util.Scanner;
public class Test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);String user = "林沐雨";String passwd = "123456";String loginUser = "";String loginPasswd = "";//登录判断for (int i = 1; i <= 3; i++) {//输入System.out.println("请输入用户名:");loginUser = sc.next();System.out.println("请输入密码:");loginPasswd = sc.next();if (loginUser.equals(user)) {if (loginPasswd.equals(passwd)) {System.out.println("登录成功~");break;} else {if (3 - i == 0) {System.out.println("账户已锁定,请稍微再试");break;}System.out.println("密码错误,你还有" + (3 - i) + "次机会");}} else {if (3 - i == 0) {System.out.println("账户已锁定,请稍微再试");break;}System.out.println("用户名错误,你还有" + (3 - i) + "次机会");}}}
}