程序流程控制
1.顺序结构
程序从上至下逐行执行,无判断与跳转
public class Test1{ public static void main(String[] args){int i = 1;int j = i + 1; System.out.println(j);}
}
2.分支结构
依据条件,选择性执行某段语句
主要有以下两种
2.1 if…else语句
单判断
public class Test2{ public static void main(String[] args){int age = 26;if(age >= 18){System.out.println("你已经成年了!");}else{System.out.println("你还是未成年!");
}}
}
多判断
public class Test3{ public static void main(String[] args){int age = 26;if(age <18){System.out.println("你还是未成年!");}else if(age > 45){System.out.println("你是中老年人!");}else{System.out.println("你是青年人!");}}
}
优化:添加键盘输入
//1.导包
import java.util.Scanner;
public class Test4{public static void main(String[] args){//2.创建Scanner类的对象Scanner s = new Scanner(System.in);System.out.println("请输入你的年龄: ");//3.调用此对象的方法next(字符串),nextInt(整型)int age = s.nextInt();if(age < 18){System.out.println("你还是未成年!");}else if(age > 45){System.out.println("你是中老年人!");}else{System.out.println("你是青年人!");}}
}
if…else分支结构测试
题目一
import java.util.Scanner;
public class Test5{public static void main(String[] args){Scanner s = new Scanner(System.in);System.out.println("请输入你的成绩: ");int score = s.nextInt();if(score == 100){System.out.println("BMW车,你拿走!");}else if(80 < score && score <= 99){System.out.println("Iphone13,你拿走!");}else if(60 <= score && score <= 80){System.out.println("参考书,你拿走!");}else{System.out.println("什么都没有了!");}}
}
题目二
方法一:
import java.util.Scanner;
public class Test { public static void main(String[] args) {// 键盘输入三个数Scanner s = new Scanner(System.in);System.out.println("第一个数: ");int num1 = s.nextInt();System.out.println("第二个数: ");int num2 = s.nextInt();System.out.println("第三个数: ");int num3 = s.nextInt();if(num1 > num2){if(num3 > num1)System.out.println("三个数从小到大排列:" + num2 +"," + num1 + "," + num3);else if(num3 < num2){System.out.println("三个数从小到大排列:" + num3 +"," + num2 + "," + num1);}elseSystem.out.println("三个数从小到大排列:" + num2 +"," + num3 + "," + num1);}else{if(num3 > num2){System.out.println("三个数从小到大排列:" + num1 +"," + num2 + "," + num3);}else if(num3 < num1){System.out.println("三个数从小到大排列:" + num3 +"," + num1 + "," + num2);}else{System.out.println("三个数从小到大排列:" + num1 +"," + num3 + "," + num2);}}}
}
方法二:
import java.util.Scanner;
public class Test{public static void main(String[] args){//1.键盘输入三个数Scanner s = new Scanner(System.in);System.out.println("第一个数: ");int num1 = s.nextInt();System.out.println("第二个数: ");int num2 = s.nextInt();System.out.println("第三个数: ");int num3 = s.nextInt();//2.判断第一个与第二个数的大小,让num2 > num1(如果num1>num2,值互换;反之就不变)if(num1 > num2){num1 = num1 ^ num2;num2 = num1 ^ num2;num1 = num1 ^ num2;} //3.判断第三个数的大小(第二大,最小,最大)if(num2 >num3 && num3 > num1){num2 = num2 ^ num3;num3 = num2 ^ num3;num2 = num2 ^ num3;System.out.println("三个数从小到大排列:" + num1 +"," + num2 + "," + num3);}else if(num2 >num3 && num3 < num1){System.out.println("三个数从小到大排列:" + num3 +"," + num1 + "," + num2);}else{System.out.println("三个数从小到大排列:" + num1 +"," + num2 + "," + num3); }}
}
注意
- 条件判断之间可以嵌套
如果多个条件语句是互斥
关系,多个条件语句上下顺序自由
;
如果多个条件语句是包含
关系,要求条件语句范围小
的要写在
范围大的上面
。 - 如果if中的执行语句只有一行,可以省略
{}
,建议不要省略。
2.2 switch语句
- switch语句会根据表达式对应case去判断,满足就执行,每条case语句一定要加break,否则程序会继续执行下去
- case 常量:常量只能是值,不能是取值范围
- default:可选,位置灵活
- 常量可取char,byte,short,int,String(jdk1.7),枚举
switch分支结构测试
题目一
根据输入的月份,判断当前季节?
import java.util.Scanner;
// 3,4,5,月为春天,6,7,8,月为夏天,9,10,11月为秋天,12,1,2月为冬天
public class Test {public static void main(String[] args) {Scanner s = new Scanner(System.in);System.out.println("请输入月份: ");int age = s.nextInt();switch(age/3){case 1:System.out.println(age + "月是春天");break;case 2:System.out.println(age + "月是夏天");break;case 3:System.out.println(age + "月是秋天");break;default:System.out.println(age + "月是冬天");break;}}
}
题目二
根据键盘输入的月份与日期,判断是2023年的第多少天?
import java.util.Scanner;
public class Test {public static void main(String[] args) {Scanner s = new Scanner(System.in);System.out.println("请输入月份: ");int month = s.nextInt();System.out.println("请输入日期: ");int day = s.nextInt();int sum = 0;switch(month){default:System.out.println("输入错误");case 12:sum += 30; //+11月case 11:sum += 31; //+10月case 10:sum += 30; //+9月case 9:sum += 31; //+8月case 8:sum += 31; //+7月case 7:sum += 30; //+6月case 6:sum += 31; //+5月case 5:sum += 30; //+4月case 4:sum += 31; //+3月case 3:sum += 28; //+2月case 2:sum += 31; //+1月case 1:sum += day; }System.out.println(month + "月" + day + "日" + "是2023年的第" + sum + "天");}
}
课外题
3.循环结构
依据条件,重复性执行某段语句
组成部分:初始化部分,循环条件部分,循环体部分,迭代部分
主要有以下三种
3.1 while语句
输出100以内的所以偶数及所有偶数的和
方法一:
public class Test { public static void main(String[] args) {int sum = 0;int i = 0;while(i <= 100){sum += i;System.out.println(i);i += 2;} System.out.println(sum);}
}
方法二:
public class Test { public static void main(String[] args) {int sum = 0;int i = 0;while(i <= 100){if(i % 2 == 0){sum += i; System.out.println(i);}i++;} System.out.println(sum);}
}
3.2 do…while语句
格式
初始化;
do{循环体;迭代条件;
}while(循环条件)
输出100以内的所以偶数及所有偶数的和
方法一:
public class Test2 { public static void main(String[] args) {int sum = 0;int i = 0;do{System.out.println(i);sum += i;i += 2;}while(i <= 100);System.out.println(sum); }
}
方法二:
public class Test1 { public static void main(String[] args) {int sum = 0;int i = 0;do{if(i % 2 == 0){sum += i; System.out.println(i);}i++;}while(i <= 100);System.out.println(sum);}
}
while语句与do-while的区别:
do-while至少执行一次
3.3 for语句(常用)
格式:
for(初始化;循环条件;迭代条件;){循环体;
}
执行过程:初始化,循环条件,循环体,迭代条件,循环条件,循环体,迭代条件,循环条件…
简单编写
public class Test1 { public static void main(String[] args) {//for循环(初始化,循环条件,迭代条件,循环体)for(int i = 1;i < 4;i++){System.out.println("欢迎光临,我的第" + i + "位客人!");} }
}
题目一
输出100以内的所以偶数及所有偶数的和
方法一:
public class Test2 {public static void main(String[] args) {int sum = 0;for(int i = 0;i <= 100;i += 2){sum += i;System.out.println(i);} System.out.println(sum);}
}
方法二:
public class Test1 { public static void main(String[] args) {int sum = 0;for(int i = 0;i <= 100;i++){if(i % 2 == 0){sum += i; System.out.println(i);}} System.out.println(sum);}
}
题目二
public class Test1 { public static void main(String[] args) {int sum = 0;for(int i = 0;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();} }
}
题目三:水仙花树
public class Test1 { public static void main(String[] args) { int sum = 0; for(int i = 100;i < 1000;i++){ int a = i/100; int b = (i/10)%10; int c = i%10; if(a * a * a + b * b * b + c * c * c == i){ System.out.println(i); }} }
}
题目四
方法一
for循环
import java.util.Scanner;
public class Test{public static void main(String[] args) {Scanner s = new Scanner(System.in);int a = 0; //正数个数int b = 0; //负数个数int c = 0; //总数个数for(;;c++){System.out.println("请输入数字");int j = s.nextInt();if(j > 0){a += 1;}else if(j < 0){b += 1;}else{break;}}System.out.println("总共输入个数:" + c + "正数个数为: " + a + "负数个数为:" + b ); }
}
方法二
while循环
import java.util.Scanner;
public class Test{public static void main(String[] args) {Scanner s = new Scanner(System.in);int a = 0; //正数个数int b = 0; //负数个数int c = 0; //总数个数while(true){System.out.println("请输入数字");int j = s.nextInt();if(j > 0){a += 1;c++;}else if(j < 0){b += 1;c++;}else{break;}}System.out.println("总共输入个数:" + c + "正数个数为: " + a + "负数个数为:" + b ); }
}
方法三
import java.util.Scanner;
public class Test{public static void main(String[] args) {Scanner s = new Scanner(System.in);int a = 0; //正数个数int b = 0; //负数个数int c = 0; //总数个数for(int i = 1;i != 0;c++){System.out.println("请输入数字");int j = s.nextInt();if(j > 0){a += 1;}else if(j < 0){b += 1;}else{i = 0;}}System.out.println("总共输入个数" + c + "正数个数为: " + a + "负数个数为:" + b + "零个数为:1"); }
}
嵌套循环
一:打印边长为5*的正方形(特殊的长方形)
public class Test{ public static void main(String[] args) {for(int i = 1;i < 6;i++){//外层控制行数for(int j = 1;j < 6;j++){//内层控制列数System.out.print("*");}System.out.println();}}
}
二:打印金字塔型
public class Test{ public static void main(String[] args) {for(int i = 0;i < 6;i++){for(int j = 0;j < 6 - i;j++){System.out.print(" ");}for(int k = 0; k < (i + 1) * 2 - 1;k++){System.out.print("*");}System.out.println();}
}
}
题目一:九九乘法表
public class Test{ public static void main(String[] args) {for(int i = 1;i < 10;i++){for(int j = 1;j <= i;j++){System.out.print(i + "*" + j + "=" + i*j + "\t");}System.out.println();} }
}
题目二:1~100之间的质数
方法一
public class Test{ public static void main(String[] args) {boolean flag = false;for(int i = 2;i <= 100;i++){for(int j = 2;j < i;j++){if(i % j == 0){flag = true;}}if(flag == false){System.out.println(i);}flag = false;}}
}
方法二(优化)
public class Test{ public static void main(String[] args) {boolean flag = false;long start = System.currentTimeMillis();//获取当前系统毫秒数for(int i = 2;i <= 1000;i++){for(int j = 2;j <= Math.sqrt(i);j++){//优化一:取根号即可确定根号i平方 == iif(i % j == 0){flag = true;break; //优化二:满足条件就退出循环}}if(flag == false){System.out.println(i);}flag = false;}long end = System.currentTimeMillis();System.out.println("总花费时间:" + (end-start));}
}
- break:结束"当前"循环;continue:结束"当次"循环
- break语句与continue语句之后不能添加其他语句
注:JDK1.5提供方便遍历集合,数组元素的foreach循环
4.练习题
感谢大家的支持,关注,评论,点赞!
参考资料:
尚硅谷宋红康20天搞定Java基础上部