流程控制
三种基本流程结构:
顺序结构分支结构循环结构
1.顺序结构
程序从上到下逐行地执行,中间没有任何判断和跳转。
2.分支结构
根据条件,选择性地执行某段代码。
有if…else和switch-case两种分支语句。
2.1 if-else
if语句有三种格式:
if(条件表达式){
执行代码块;
}
if(条件表达式){
执行代码块1;
}
else{
执行代码块2;
}
if(条件表达式1){
执行代码块1;
}
else if(条件表达式2){
执行代码块2;
}
…
else{
执行代码块n;
}
说明:
else结构是可选的。
针对于条件表达式:
如果多个条件表达之间是“互斥”关系(或没有交集),条件判断和执行语句声明在上面还是下面,无所谓。如果多个条件表达之间有交集关系,需要根据实际情况,考虑应该将哪个结构声明在上面。如果多个条件表达之间有包含关系,通常情况下,需要将范围小的声明在范围大的上面。否则,范围小的就没机会执行。
if-else结构是可以相互嵌套的。
如果if-else结构中的执行语句只有一行时,对应的一对{}可以省略的。但是,不建议省略。
条件表达式必须是布尔表达式(关系表达式或逻辑表达式)、布尔变量。
练习:
/*
岳小鹏参加Java考试,他和父亲岳不群达成承诺:
如果:
成绩为100分时,奖励一辆BMW;
成绩为(80,99]时,奖励一台iphone xs max;
当成绩为[60,80]时,奖励一个 iPad;
其它时,什么奖励也没有。
请从键盘输入岳小鹏的期末成绩,并加以判断
*/
import java.util.Scanner;
class IfTest{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入岳小鹏期末成绩:(0-100)");
int score = scan.nextInt();
if( score == 100){
System.out.println("奖励一辆BMW");
}else if(score>80 && score<=99){
System.out.println("奖励一台iphone xs max");
}else if(score >=60 && score <= 80){
System.out.println("奖励一个 iPad");
}else{
System.out.println("什么奖励也没有");
}
}
}
//课后练习:如何获取一个随机数:10 - 99
int value = (int)(Math.random() * 90 + 10);// [0.0,1.0) --> [0.0,90.0) --->[10.0, 100.0) -->[10,99]
System.out.println(value);
//公式:[a,b] : (int)(Math.random() * (b - a + 1) )+ a
/*
大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出一定的条件:
高:180cm以上;富:财富1千万以上;帅:是。
如果这三个条件同时满足,则:“我一定要嫁给他!!!”
如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”
如果三个条件都不满足,则:“不嫁!”
*/
import java.util.Scanner;
class IfExer1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入你的身高:(cm)");
int height = scan.nextInt();
System.out.println("请输入你的财富:(千万)");
double wealth = scan.nextDouble();
//方式一:
System.out.println("请输入你是否帅:(true/false)");
boolean isHandsome = scan.nextBoolean();
if(height >= 180 && wealth >= 1 && isHandsome){
System.out.println("我一定要嫁给他!!!");
}else if(height >= 180 || wealth >= 1 || isHandsome){
System.out.println("嫁吧,比上不足,比下有余。");
}else{
System.out.println("不嫁!");
}
//方式二:
System.out.println("请输入你是否帅:(是/否)");
String isHandsome = scan.next();
if(height >= 180 && wealth >= 1 && isHandsome.equals("是")){
System.out.println("我一定要嫁给他!!!");
}else if(height >= 180 || wealth >= 1 || isHandsome.equals("是")){
System.out.println("嫁吧,比上不足,比下有余。");
}else{
System.out.println("不嫁!");
}
}
}
2.2 switch-case
格式:
switch(表达式){
case 常量1:
执行语句1;
break;
case 常量2:
执行语句2;
break;
...
default:
执行语句n;
break;
}
说明:
根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。当调用完执行语句以后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字或此switch-case结构末尾结束为止。
break,可以使用在switch-case结构中,表示一旦执行到关键字,就跳出switch-case结构。
break关键字是可选的。
switch结构中的表达式,只能是如下6种数据类型之一:
byte、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)
case子句中的值必须是常量,不能是变量名或不确定的表达式值。
default:相当于if-else结构中的else。同时,位置也是灵活的。当没有匹配的case时,执行default。
注意:
凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。我们写分支结构时,当发现既可以使用switch-case,(同时,switch中表达式的取值情况不太多),又可以使用if-else时,我们优先选择使用switch-case。原因:switch-case执行效率稍高。如果switch-case结构中的多个case的执行语句相同,则可以考虑进行合并。
练习:
/*
例题:对学生成绩大于60分的,输出“合格”。低于60分的,输出“不合格”。
说明:如果switch-case结构中的多个case的执行语句相同,则可以考虑进行合并。
*/
class SwitchCaseTest{
public static void main(String[] args){
int score = 78;
switch(score/10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
System.out.println("不及格");
break;
case 7:
case 8:
case 9:
case 10:
System.out.println("及格");
break;
}
//更优的解决方案:
switch(score/60){
case 0:
System.out.println("不及格");
break;
case 1:
System.out.println("及格");
break;
}
}
}
/*
从键盘分别输入年、月、日,判断这一天是当年的第几天
注:判断一年是否是闰年的标准:
1)可以被4整除,但不可被100整除
或
2)可以被400整除
说明:
1. 凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。
2. 我们写分支结构时,当发现既可以使用switch-case,(同时,switch中表达式的取值情况不太多),又可以使用if-else时,我们优先选择使用switch-case。原因:switch-case执行效率稍高。
*/
import java.util.Scanner;
class SwitchCaseExer{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入year:");
int year = scan.nextInt();
System.out.println("请输入month:");
int month = scan.nextInt();
System.out.println("请输入day:");
int day = scan.nextInt();
//定义一个变量来保存总天数
int sumDays = 0;
Switch(month){
case 12:
sumDays += 30;
case 11:
sumDays += 31;
case 10:
sumDays += 30;
case 9:
sumDays += 31;
case 8:
sumDays += 31;
case 7:
sumDays += 30;
case 6:
sumDays += 31;
case 5:
sumDays += 30;
case 4:
sumDays += 31;
case 3:
//判断year是否是闰年
if(((year % 4 == 0) && year % 100 != 0) || year % 400 = 0){
sumDays += 29;
}else{
sumDays += 28;
}
case 2:
sumDays += 31;
case 1:
sumDays += day;
}
System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDays + "天");
}
}
3.循环结构
循环结构
在某些条件满足的情况下,反复执行特定代码的功能
循环语句分类
for 循环while 循环do-while 循环
3.1 for 循环
循环结构的4个要素
初始化条件循环条件 —>是boolean类型循环体迭代条件
for循环结构
for(1(初始化条件);2(循环条件);4(迭代条件)){3(循环体);
}
执行过程:1 - 2 - 3 -4 - 2 - 3 - 4 - 2 - 3 - 4 - … - 2
练习:
/*
编写程序从1循环到150,并在每行打印一个值,
另外在每个3的倍数行上打印出“foo”,
在每个5的倍数行上打印“biz”,
在每个7的倍数行上打印输出“baz”。
*/
class ForTest1 {
public static void main(String[] args) {
for(int i = 1;i <= 150;i++){
System.out.print(i + " ");
if(i % 3 == 0){
System.out.print("foo ");
}
if(i % 5 == 0){
System.out.print("biz ");
}
if(i % 7 == 0){
System.out.print("baz ");
}
//换行
System.out.println();
}
}
}
//例题:遍历100以内的偶数,输出所有偶数的和,输出偶数的个数
int sum = 0;//记录所有偶数的和
int count = 0;//记录偶数的个数
for(int i = 1;i <= 100;i++){
if(i % 2 == 0){
System.out.println(i);
sum += i;
count++;
}
//System.out.println("总和为:" + sum);
}
System.out.println("总和为:" + sum);
System.out.println("个数为:" + count);
3.2 while 循环
语法格式
①初始化部分while( ②循环条件部分){ {
③分 循环体部分;
④分 迭代部分;
}
执行过程:
①- ②- ③- ④- ②- ③- ④- ②- ③- ④-…-②
说明
注意不要忘记声明④迭代部分。否则,循环将不能结束,变成死循环。for循环和while循环可以相互转换。
练习:
//遍历100以内的所有偶数
class WhileTest{
public static void main(String[] args){
int i = 100;
while(i <= 100){
if(i % 2 == 0){
System.out.println(i);
}
i++;
}
}
//出了while循环以后,仍可以调用。
System.out.println(i);//输出101
}
3.3 do-while
语法格式:
①分 初始化部分; do{ ③ 循环体部分 ④ 迭代部分 }while( ②循环条件部分);
执行过程:
① - ③ - ④ - ② - ③ - ④ - ② - ③ - ④ - … - ②
注意:
do-while 循环至少执行一次循环体。
练习:
class DoWhileTest {
public static void main(String[] args) {
//遍历100以内的偶数,并计算所有偶数的和及偶数的个数
int num = 1;
int sum = 0;//记录总和
int count = 0;//记录个数
do{
if(num % 2 == 0){
System.out.println(num);
sum += num;
count++;
}
num++;
}while(num <= 100);
System.out.println("总和为:" + sum);
System.out.println("个数为:" + count);
//体会do-while至少执行一次循环体
int number1 = 10;
while(number1 > 10){
System.out.println("hello:while");
number1--;
}
int number2 = 10;
do{
System.out.println("hello:do-while");
number2--;
}while(number2 > 10);
}
}
4.break和continue关键字
使用范围作用特点breakswitch-case、循环结构中结束当前循环关键字后面不能声明执行语句continue循环结构中结束本次循环关键字后面不能声明执行语句
**注意:**break、continue之后不能有其他的语句,因为程序永远不会执行其后的语句。
练习:
class BreakContinueTest {
public static void main(String[] args) {0
for(int i = 1;i <= 10;i++){
if(i % 4 == 0){
break;//执行break输出:123
//continue;//执行continue输出:123567910
//System.out.println("今晚出去High!!!");
}
System.out.print(i);
}
System.out.println(" ");
//******************************
label:for(int i = 1;i <= 4;i++){
for(int j = 1;j <= 10;j++){
if(j % 4 == 0){
//break;//默认跳出包裹此关键字最近的一层循环。
//continue;
//break label;//结束指定标识的一层循环结构
continue label;//结束指定标识的一层循环结构当次循环
}
System.out.print(j);
}
System.out.println();
}
}
}
5.循环嵌套
不在循环条件部分限制次数的结构:for(; ; ; ) 或 while(true)结束循环有几种方式? 方式一:循环条件部分返回false 方式二:在循环体中,执行break
import java.util.Scanner;
class ForWhileTest {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int positiveNumber = 0;//记录正数的个数
int negativeNumber = 0;//记录负数的个数
for(;;){//while(true){
int number = scan.nextInt();
//判断number的正负情况
if(number > 0){
positiveNumber++;
}else if(number < 0){
negativeNumber++;
}else{
//一旦执行break,跳出循环
break;
}
}
System.out.println("输入的正数个数为:" + positiveNumber);
System.out.println("输入的负数个数为:" + negativeNumber);
}
}
1.嵌套循环的使用
嵌套循环:将一个循环结构A声明在另一个循环结构B的循环体中,就构成了嵌套循环。外层循环:循环结构B 内层循环:循环结构A
2.说明
内层循环结构遍历一遍,只相当于外层循环循环体执行了一次假设外层循环需要执行m次,内层循环需要执行n次。此时内层循环的循环体一共执行了m * n次
3.技巧
外层循环控制行数,内层循环控制列数
练习:
class ForForTest{
public static void main(String[] args){
//******
System.out.println("******");
System.out.println(" ");
for(int i = 1;i <= 6;i++){
System.out.print('*');
}
System.out.println(" ");
/*
******
******
******
******
*/
for(int j = 1;j <= 4;j++ ){
for(int i = 1;i <= 6;i++){
System.out.print('*');
}
System.out.println();
}
/*i(行号)j(*的个数)
*11
**22
***33
****44
*****55
*/
for(int i = 1;i <= 5;i++){//控制行数
for(int j = 1;j <= i;j++){//控制列数
System.out.print("*");
}
System.out.println();
}
/*i(行号)j(*的个数) 规律:i + j = 5 换句话说:j = 5 - i;
****14
***23
**32
*41
*/
for(int i = 1;i <= 4;i++){
for(int j = 1;j <= 5 - i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_45143657/article/details/109757213