Java1.5入门-顺序控制结构

程序流程控制介绍

在程序中,程序运行的流程控制决定程序是如何执行的,是我们必须掌握的。
(1)顺序控制
(2)分支控制
(3)循环控制

1、顺序控制

1.1、顺序控制介绍

程序从上到下逐行执行,中间没有任何判断和跳转。

1.2、顺序控制举例和注意事项

依次执行:执行语句1---->执行语句2---->…… ---->执行语句n

//正确形式
public class Test {
	int num1 = 12;
	int num2 = num1 + 2; //正确
}
//错误形式
public class Test {
	int num2 = num1 + 2; //错误
	int num1 = 12;
}

2、分支控制

2.1、单分支
2.1.1、基本语法
if(条件表达式) {
	执行代码块;(可以有多条语句)
}

说明:当条件表达式为true是就会执行{ }的代码。如果为false,就不会执行。
特别说明:如果{ }中只有一条语句,则可以不用{ },建议是写上{ }

-----单分支&案例说明----
//可以输入人的成绩,80分以上则输出,你太优秀了,否则什么都不输出
import java.util.Scanner;
public class If01 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入成绩");
		int score = scanner.nextInt();
		if(score >=80) {
			System.out.println("你太优秀了!!!");
		}
	}
}
2.1.2、单分支流程图

单分支流程图

2.2、双分支
2.2.1、基本语法
if(条件表达式) {
	执行代码块1;
} else {
	执行代码块2;
}

说明:当条件表达式为true,执行代码块1,否则执行代码块2。
特别说明:如果{ }中只有一条语句,则可以不用{ },建议是写上{ }。

-----双分支&案例说明----
	//可以输入人的成绩,60分以上及格,以下不及格。
import java.util.Scanner;
public class If02 {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入成绩");
		int score = scanner.nextInt();
		if(score >= 60) {
			System.out.println("及格");
		} else {
			System.out.println("不及格");
		}
	}
}
2.2.2、双分支流程图

双分支流程图

2.3、多分支
2.3.1、基本语法
if(条件表达式1) {
	执行代码块1;
} else if(条件表达式2) {
	执行代码块2;
}
……
else {
	执行代码块n;
}

说明:
(1)当条件表达式1成立是,即执行代码块1,
(2)如果表达式1不成立,才回去判断表达式2是否成立,
(3)如果表达式2成立,就会执行代码块2,
(4)以此类推,若果所有的表达式都不成立,
(5)则执行else的代码块,注意,只能有一个执行入口。

2.3.2、多分支流程图

多分支流程图

2.4、嵌套分支
2.4.1基本介绍

在一个分支结构中又完整的潜逃了了另一个完整的分支结构,里面的分支的结构称为内层分支碗面的分支结构称为外层分支,规范:不要超过3层(可读性差)

2.4.2基本语法
if() {
	if() {
		if……else……
	}else {
		if……else……
	}
}
-----嵌套分支&案例说明----
//参加比赛,如果初赛成绩大于8.0进入决赛否则淘汰。并且根据性别提示进入男子组还是女子组。输入成绩和性别,进行判断和输出信息。
import java.util.Scanner;
public class NetedIf {
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入该人员的成绩");
        double score = scanner.nextDouble();
        if(score > 8.0) {
            System.out.println("请输入该人员的性别");
            char gender = scanner.next().charAt(0);
            if(gender == '男') {
                System.out.println("进入男子组");
            }else if(gender == '女') {
                System.out.println("进入女子组");
            }else {
            	System.out.println("你的性别有误,不能参加决赛");
            }
        }else {
            System.out.println("你被淘汰了");
        }
    }
}
/**
 *
 *出票系统:根据淡旺季的月份和年龄,打印票价。
 *4-10月份,旺季:成人(18-60):60元,儿童18岁以下:半价30元,老人(>60):1/3票价,20元
 *其余为淡季:成人:40元,其他:20元
 */
import java.util.Scanner;
public class NetedIf01 {
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入当前月份");
        double month = scanner.nextDouble();
        if (month >= 4 && month <= 10) {
            System.out.println("旺季:");
            System.out.println("请输入该人员的年龄");
            int sex = scanner.nextInt();
            if (sex < 18) {
                System.out.println("儿童半价票30元");
            } else if (sex >= 18 && sex <= 60) {
                System.out.println("成人60元");
            } else if (sex > 60) {
                System.out.println("老人1/3票价20元");
            } else {
                System.out.println("年龄输入有误");
            }
        } else {
            System.out.println("淡季:");
            System.out.println("请输入该人员的年龄");
            int sex = scanner.nextInt();
            if (sex >= 18 && sex <= 60) {
                System.out.println("成人40元");
            } else {
                System.out.println("其他20元");
            }
        }
    }
}
2.5、switch分支结构

(1)switch关键字,表示swtich分支
(2)表达式对应一个值
(3)case 常量1:当表达式的值等于常量1,就执行语句块1
(4)break:表示退出swtich
(5)如果和case常量1匹配,就执行语句块1,如果没有匹配,就继续匹配case常量2
(6)如果一个都没有匹配上,执行default

2.5.1、基本语法
switch(表达式) {
	case 常量1:
		语句块1;
		break;
	case 常量2:
		语句块2;
		break;
	……
	case 常量n:
		语句块n;
		break;
	default:
		default语句块;
		break;
}
2.5.2、switch流程图

switch流程图

-----switch分支&案例说明----
/**
 *
 *根据用户输入a-g打印出对应的星期,a=星期一,b=星期二……。
 *
 */
import java.util.Scanner;
public class Switch01 {
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个字符");
        char str = scanner.next().charAt(0);
        switch(str) {
            case 'a':
                System.out.println("星期一");
                break;
            case 'b':
                System.out.println("星期二");
                break;
            case 'c':
                System.out.println("星期三");
                break;
            case 'd':
                System.out.println("星期四");
                break;
            case 'e':
                System.out.println("星期五");
                break;
            case 'f':
                System.out.println("星期六");
                break;
            case 'g':
                System.out.println("星期天");
                break;
            default:
                System.out.println("输入有误!!!");
                break;
        }
    }
}
2.5.3、switch细节

(1)表达式数据类型,硬核case后的常量类型一致,或者是可以自动转成可以相互比较的类型,比如输入的是字符,而常量是int
(2)switch(表达式)中的表达式的返回值必须是:(byte,short,int,char,enum,String)

double c = 1.1;
switch(c) { // 错误
	case 1.1:
		 System.out.println("ok");
		break;
}

(3)case子句中的值必须是常量,而不能是变量
(4)default子句是可选的,当没有匹配的case时,执行default
(5)break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有写break,程序会顺序执行到switch结尾

-----switch分支&小案例----
//使用switch把小写类型的char型转为大写a-e,其他的输出other
import java.util.Scanner;
public class SwitchExercise {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入a-e");
		char c1 = scanner.next().charAt(0);
		switch(c1) {
			case 'a':
				System.out.println("A");
                break;
            case 'b':
            	System.out.println("B");
                break;
            case 'c':
            	System.out.println("C");
                break;
            case 'd':
            	System.out.println("D");
                break;
            case 'e':
	            	System.out.println("E");
                break;
            default:
            	System.out.println("other");
                break;
		}
	}
}
//对学生成绩大于60分的,输出“合格”。低于60分的输出“不合格”,成绩不得大于100,提示成绩/60
import java.util.Scanner;
public class SwitchExercise {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生成绩");
		double score = scanner.nextDouble();
		if(score >= 0 && score <= 100) {
			switch((int)(score / 60)) {
				case 0:
					System.out.println("不及格");
                	break;
            	case 1:
            		System.out.println("及格");
                	break;
            	default:
            		System.out.println("other");
                	break;
			}
		}else {
			System.out.println("输入成绩无效");
		}
	}
}
//根据用户指定月份,打印该月份的季节,穿透写法
import java.util.Scanner;
public class SwitchExercise {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入学生成绩");
		int month = scanner.nextInt();
		switch(month) {
            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;
            case 12:
            case 1:
            case 2:
				System.out.println("冬季");
                break;
            default:
            	System.out.println("输入月份无效");
                break;
		}
	}
}
2.6、switch和if的选择

(1)如果判断的具体数值不多,而且符合byte、short、int、char、enum、String这6中类型。虽然两个语句都可以使用,建议使用swtich语句。
(2)其他情况:对区间判断,对结果为boolean类型判断,使用if,if的适用范围更广。

3、循环控制

就是让你的代码可以循环的执行

3.1、for循环
3.1.1、基本语法
for(循环变量初始化; 循环条件; 循环变量迭代){
	循环操作(可以多条语句);
}
3.1.2、说明

(1)for关键字,表示循环控制
(2)for有四要素:(1)循环变量初始化(2)循环条件(3)循环操作(4)循环变量迭代
(3)循环操作,这里可以有多条语句,也就是我们要循环执行的代码

特别说明

如果,循环操作只有一条语句,可以省略{},建议不要省略

-----for循环分支&小案例----
//打印10句“你好,For循环”
public class For01 {
	public static void main(String[] args) {
		for(int i = 0; i < 10; i++){
			System.out.println("你好,For循环");
		}
	}
}
3.1.3、for循环流程图

for循环流程图

3.1.4、for循环注意事项和细节说明

(1)循环条件是返回一个布尔值得表达式。
(2)for(;循环判断条件;)中的初始化和变量迭代可以写到其他地方,但是两边的分号不能省略。
(3)循环初始值可以有多条初始化语句,但要求类型一样,并且中间用逗号隔开,循环变量迭代也可以有多变量迭代语句,中间用逗号隔开。
(4)使用内存分析法,分析下面代码输出内容

int count = 3;
for(int i =0;j = 0;i < count;i++,j+=2) {
	Sytem.out.println("i="+i+",j="+j); //i=0,j=0------i=1,j=2------i=2,j=4
}
-----for循环分支&课堂练习----
//打印1~100之间所有9的倍数的整数,统计个数及总和
public class ForExercise {
	public static void main(String[] args) {
        int j = 0;
        int sum = 0;
        for(int i = 1; i <= 100; i++){
            if(i % 9 == 0){
                System.out.println("i=" + i);
                j++;
                sum += i;
            }
        }
        System.out.println("个数为" + j);
        System.out.println("总和为" + sum);
    }
}
//打印出5以内加法等于5的输出
public class ForExercise {
	public static void main(String[] args) {
        for(int i = 0,j = 5; i <= 5; i++,j--){
            System.out.println(i + " + " + j + " = "+ (i + j));
        }
    }
}
3.2、while循环控制
3.2.1、基本语法
while(循环条件) {
	循环体(语句);
	循环变量迭代;
}
3.2.2、说明

(1)while循环也有四要素:(1)循环变量初始化(2)循环条件(3)循环操作(4)循环变量迭代
(2)只是四要素放的位置不一样

3.2.3、while循环流程图

while循环流程图

-----while循环&小案例----
//打印10句“你好,while循环”
public class While01 {
	public static void main(String[] args) {
		int i = 0; //循环变量初始化
		while(i < 10){	//循环条件
			System.out.println("你好,while循环"); //执行语句
			i++; //循环变量迭代
		}
	}
}
-----while循环&课堂练习----
//打印1-100之间所有能被3整除的数(使用while)
public class WhileExercise01 {
	public static void main(String[] args) {
		int i = 1; //循环变量初始化
		while(i <= 100){	//循环条件
			if(i % 3 == 0) {
				System.out.println(i); //执行语句
			}
			i++; //循环变量迭代
		}
	}
}
//打印40-200之间所有的偶数(使用while)
public class WhileExercise02 {
	public static void main(String[] args) {
		int i = 40; //循环变量初始化
		while(i <= 200){	//循环条件
			if(i % 2 == 0) {
				System.out.println(i); //执行语句
			}
			i++; //循环变量迭代
		}
	}
}
3.3、do…while循环控制
3.3.1、基本语法
循环变量初始化;
do{
	循环体(语句);
	循环变量迭代;
}while(循环条件);
3.3.2、说明

(1)do…while是关键字
(2)也有循环四要素,只是位置不一样:(1)循环变量初始化(2)循环条件(3)循环操作(4)循环变量迭代
(3)先执行,在判断,也就是说,一定会执行一次
(4)最后有一个分号;
(5)while和do…while有区别

3.3.3、do…while循环流程图

do…while循环流程图

-----do…while循环&小案例----
public class DoWhile01 {

	public static void main(String[] args) {
		int i = 1; // 循环变量初始值
		do{
			// 循环执行语句
			System.out.println("你好do……while");
			// 循环变量迭代
			i++;
		}while(i <= 10);
	}
}
3.3.4、do…while注意事项和细节

(1)循环条件是返回一个布尔值的表达式
(2)do…while循环是先执行,在判断,因此他至少执行一次

-----do…while循环&课堂练习----
// 打印1-100
public class DoWhile02 {
		public static void main(String[] args) {
			int i = 1;
			do {
				System.out.println(i);
				i++;
			}while(i <= 100);
		}
}
//计算1-100的和
public class DoWhile03 {
		public static void main(String[] args) {
			int i = 1;
			int sum = 0;
			do {
				sum += i;
				i++;
			}while(i <= 100);
			System.out.println(sum);
		}
}
//统计1-200之间能被5整除但不能被3整除的个数
public class DoWhile04 {
		public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        do {
            if (i % 5 == 0 && i % 3 != 0) {
                sum++;
                System.out.println(i + "------" + sum + "个");
            }
            i++;
        } while (i <= 200);
        System.out.println("一共:" + sum + "个");
    }
}
//如果李四不还钱,则王五一直使出五连鞭,直到李四说还钱为止
import java.util.Scanner;

public class DoWhile04 {
    public static void main(String[] args) {
        char answer = ' ';
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("给了李四一次五连鞭!!!!");
            System.out.println("王五问:还钱吗");
            answer = scanner.next().charAt(0);
        } while (answer != 'y');
        System.out.println("李四还了钱!");
    }
}

4、多重循环控制

(1)讲一个循环放在另一个循环体内,就形成了嵌套循环。其中for、while、do…while均可以作为外层循环和内层循环。
(2)建议一般使用两层,最多不要超过三层。
(3)实质上,嵌套循环就是把内层循环当成外层循环的循环体。当只有内层循环条件为false时,才会完全跳出内层循环,开始下一次的循环。
(4)设外层循环次数为9次,内层为9次,则内层循环体实际上执行了9*9次。

-----多重循环&小案例----
for (int i = 1; i <= 9; i++) {
    for (int j = 1; j <= 9; j++) {
        if (j <= i) {
            System.out.println(j + "*" + i + "=" + j * i);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨兼程1217

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值