1.流程控制语句 顺序结构 程序从上到下逐行地执行,中间没有任何判断和跳转。 分支结构 根据条件,选择性地执行某段代码。 有if…else和switch-case两种分支语句。 循环结构 根据循环
1.流程控制语句
- 顺序结构
程序从上到下逐行地执行,中间没有任何判断和跳转。 - 分支结构
根据条件,选择性地执行某段代码。
有if…else和switch-case两种分支语句。 - 循环结构
根据循环条件,重复性的执行某段代码。
有while、do…while、for三种循环语句。
注:JDK1.5提供了foreach循环,方便的遍历集合、数组元素。
2.顺序结构
Java中定义成员变量时采用合法的前向引用。如:
int num1 = 12;
int num2 = num1 + 2;
}
错误形式:
public class Test{int num2 = num1 + 2;
int num1 = 12;
}
语句:以分号结束的一句话称为一个条语句。
注意:没有写任何代码,只是一个分号,也是一条语句。叫做空语句
3.分支结构
3.1 IF单分支结构
- 练习1:编写程序,声明2个int型变量并赋值。判断两数之和,如果大于等于50,打印“hello world!”
- 练习2:已知年份 int year = 2001,判断 year是不是闰年,如果是闰年,则在控制台输出 是闰年。(能被4整除,不能被100整除的年份是闰年,但如果能被400整除,也是闰年)。
3.2 IF双分支结构
- 练习1:小毛参加考试,如果成绩>=60分,输出“成绩及格“。如果成绩<60分,输出”成绩不及格“。
- 练习2:从键盘输入整数,如果是奇数,输出是奇数。如果是偶数,输出是偶数
3.3 IF多分支结构
- 练习1:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入一个x值:");
int x = input.nextInt();
int y = 0;
if (x > 0) {
y = x * x - 1;
} else if (x == 0) {
y = x * x * x - 1;
}else{
y=2*x+1;
}
System.out.println("y的值是:"+y);
}
}
- 练习2:
* 练习:
提示用户输入一个月份,判断当前月份是哪一个季节?
春天 3-5
夏季 6-8
秋季 9-11
冬季 12-1
*/
public class Demo4 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入一个月份:");
int month=input.nextInt();
if(month>=3&&month<=5){
System.out.println("当前季节为春天!");
}else if(month>=6&&month<=8){
System.out.println("当前季节为夏天!");
}else if(month>=9&&month<=11){
System.out.println("当前季节为秋天!");
}else if(month==12||month==1||month==2){
System.out.println("当前季节为冬天!");
}else{
System.out.println("输入的月份有误!");
}
System.out.println("程序结束!");
}
}
3.4 IF语句嵌套结构
4.分支结构Switch
- 语法结构:
- 示例1:
- 注意事项:
switch(表达式)中表达式的值必须是下述几种类型之一:byte,short,char,int,枚举,String;
switch尽量不要省略break语句,会造成一个“穿透”现象。没有break无法退出。
break作用就是退出switch语句,可以省略,但是会出问题!会造成穿透现象! - 如果去掉break,那么会造成“穿透”现象!
case 后面的值不能重复!!
default位置可以挪动,但是会影响执行顺序。 - Default的语句可以换地方,但是会影响执行的顺序!一般derfault在最后!
- 老的JDK版本(JDK1.6之前的版本)不支持String类型!!新的JDK版本支持 String类型!
5.switch和if语句的对比
- 案例1:根据用于指定月份,打印该月份所属的季节。3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
* 练习:
提示用户输入一个月份,判断当前月份是哪一个季节?
春天 3-5
夏季 6-8
秋季 9-11
冬季 12-1
*/
public class Demo8 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.print("请输入一个月份:");
int month=input.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:编写程序:从键盘上读入一个学生成绩,存放在变量score中,根据score的值输出其对应的成绩等级:
score>=90 等级:A
70<=score<90 等级: B
60<=score<70 等级: C
score<60 等级: D - 案例3:编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于12年一个周期,每年用一个动物代表:rat、ox、tiger、rabbit、dragon、snake、horse、sheep、monkey、rooster、dog、pig。
提示:2017年:鸡 2017 % 12 == 1
6.循环结构
- for循环
- while循环
- do…while循环
6.1 while循环
- 语法:
- 执行过程:
先判断逻辑表达式的值,若为true则执行其后面的代码块,然后再判断条件并反复执行,直到条件不成立为止;若为false则直接退出while循环。 - 循环语句的要素:
循环的初始条件
循环结束的条件
循环执行的内容
While循环的特点:
先判断再执行循环体!
while循环有可能循环体一次也不执行!!!! - 练习1:输出1到100
- 练习2:在控制台输出1,3,5,7,9
- 案例3:输出1到100之间能被3整除的数
6.2 do-while循环
- 语法:
- 执行过程:
先执行代码块,再判断逻辑表达式的值,若为true,再执行代码块,否则退出循环 - do…while的特点:
- 先执行,再判断!
- do…while循环体至少要执行一次!!!!
- 案例1:
- 案例2: 循环计算一下1-10000之间能被3或5整除的数的和,并统计总共有多少个这样的数。
- 案例3: 输出1945年-2018年 中所有的闰年?
6.3 while循环和do-while循环的区别
7.for循环
- 语法
- 案例1:求出1到100的和
- 案例2:输出1到100之间能被3整除的数
- 案例3: 求5的阶乘
- 案例4:打印100-999之间的所有水仙花数,水仙花是指一个数的个位的立方和加上十位的立方和加上百位的立方和等于这个数本身。
例如:153=111+555+333;
for (int i = 100; i <= 999; i++) {
int ge = i % 10; // 个位数字
int shi = i / 10 % 10;// 十位数字
int bai = i / 100; // 百位数字
int result = ge * ge * ge + shi * shi * shi + bai * bai * bai;
if(result==i){
System.out.println(i);
}
}
}
8.break和continue关键字
8.1 break关键字:
- 作用:在循环中break关键字是跳出循环语句。
break在swtich和循环语句中出现 - 示例:
8.2 continue关键字:
- 再循环中,continue关键字作用是中断本次循环,继续下次循环。
- continue关键字只能出现在循环语句中
- 示例
9.案例练习
- 案例1:判断一个数是不是素数
改进上面的代码 - 案例2:输出 1 到 100 之间能被 3 整除的前 5个数
- 案例3:打印1到10个数,遇到4的倍数程序自动退出
- 案例4:打印1到10中的偶数(使用continue)
- 案例5:打印1到10中非4的倍数的数字(使用continue)
- 案例6:鸡兔同笼问题:50个头,120个脚 多少鸡? 多少兔?暴力破解法
- 案例7:最大公约数
- 案例8:最小公被数
- 案例9:一张白纸 0.5mm, 珠穆朗玛高度 8848m, 问把一张纸对折多少次之后,可以超过珠穆朗玛峰的高低?
public static void main(String[] args) {
double paper = 0.5; // 纸的厚度
double shan = 8848 * 1000; // 珠穆朗玛峰的高度
int count = 0; // 存储次数
while (paper <= shan) {
count++;
paper = paper * 2;
System.out.println("对折"+count+"次,高度是:"+paper);
}
System.out.println("对折次数是:"+count);
}
}
10.循环嵌套
将一个循环放在另一个循环体内,就形成了嵌套循环。其中,for ,while ,do…while均可以作为外层循环和内层循环。
循环嵌套的时候: 外层先执行一次,然后内层循环执行一遍!
- 例1:使用循环打印
*****
*****
*****
*****
*****
*****
public static void main(String[] args) {
for(int i=1;i<=6;i++){
for(int j=1;j<=5;j++){
System.out.print("*");
}
System.out.println();//换行
}
}
}
- 例2:使用循环打印
*
**
***
****
*****
******
public static void main(String[] args) {
for(int i=1;i<=6;i++){
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();//换行
}
}
}
- 例3:使用循环打印
******
*****
****
***
**
*
public static void main(String[] args) {
for(int i=1;i<=6;i++){
for(int j=6;j>=i;j--){
System.out.print("*");
}
System.out.println();//换行
}
}
}
- 例4:使用循环打印
for (int i = 1; i<=6; i++) {
for(int j=5;j>=i;j--){
System.out.print(" ");
}
for(int n=1;n<=2*i-1;n++){
System.out.print("*");
}
System.out.println();
}
}
- 例5:使用循环打印九九乘法表
* 九九乘法表
*/
public class Demo6 {
public static void main(String[] args) {
for (int i = 1; i<=9; i++) {
for (int j =1; j <=i; j++) {
System.out.print(j+"*"+i+"="+(j*i)+"\t");
}
System.out.println();
}
}
}
- 案例6:使用循环完成下面练习
Scanner input = new Scanner(System.in);
int sum = 0; // 求和
for (int i = 1; i <= 4; i++) {
System.out.println("开始输入第" + i + "个班的学生成绩.........");
for (int j = 1; j <= 5; j++) {
System.out.print("请输入第" + j + "个学生成绩:");
int score = input.nextInt();
sum = sum + score;// 累加分数到sum变量上
}
}
System.out.println("所有班级的分数总和为:"+sum);
System.out.println("所有班级的分数平均分为"+sum/20.0);
}