(一)介绍
任何复杂的程序逻辑都可以通过“顺序”、“分支”、“循环”这三种基本结构来实现。
顺序结构最简单,这里不多介绍
(二)分支(If else/switch)
分支结构主要包括:if、switch。
1.if语句
(1)if语句的基本结构:
if(布尔表达式){
满⾜时执⾏语句
}else{
不满⾜
}
(2) if嵌套结构:
if (boolean) {
代码块
} else if (boolean) {
代码块
} else if (boolean) {
代码块
} else { // 默认情况
代码块
}
(3) 需要注意的是:
①If~else 语句最好不要超过三层嵌套
②If(){
Return ;
}
表示结束判断,不在向下执⾏操作!!!!
③能⽤ if(){}并列的,不要⽤ if 嵌套!!
(4)卫语句:
把⾮法的操作,⽤ if 语句并列在前⾯,只要有异常操作就结束操作,然后把正常操作的逻辑写在下⾯
(5)小练习
①题目:
从控制台输入一个成绩,进行判断:
>=90 <=100 优秀
>=80 <90 良好
>=70 <80 一般
>=60 <70 及格
<60 不及格
public static void main() {
// >=90 <=100 优秀
// >=80 <90 良好
// >=70 <80 一般
// >=60 <70 及格
// <60 不及格
// int score = 48;
// 从控制台读取用户输入订单值
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
// 卫操作
// 非法的输入,直接返回
if (score < 0 || score > 100) {
System.out.println("非法输入");
return;// return 方法结束,后面代码不再执行
}
if (score >= 90 && score <= 100) {
System.out.println("优秀");
} else if (score >= 80 && score < 90) {
System.out.println("良好");
} else if (score >= 70 && score < 80) {
System.out.println("一般");
} else if (score >= 60 && score < 70) {
System.out.println("及格");
} else { // 上面都是false,else就是默认情况
System.out.println("不及格");
}
}
②题目
计算某年是不是闰年:
1、能被4整除,但是不能被100整除
||
2、能被400整除
提示:
if (() || ()) {
}
public void test993() {
System.out.println("请输入年份");
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + "是闰年");
} else {
System.out.println("不是闰年");
}
}
2、switch语句
(1)switch介绍
switch 不能⽤布尔表达式,之前只能⽤整数,现在可以⽤于枚举和 string 语句;
switch(整数|字符|枚举|string){
case:内容
break;
。。。。。
Default
}
一般情况每一个case,对应一个break;否则会出现问题。
如果没有break,满足case条件,处理之后,会继续向下执行,直到遇到break退出
(2)小练习
①题目:
从控制台输入一个成绩,进行判断:
>=90 <=100 优秀
>=80 <90 良好
>=70 <80 一般
>=60 <70 及格
<60 不及格
public static void main() {
// >=90 <=100 优秀
// >=80 <90 良好
// >=70 <80 一般
// >=60 <70 及格
// <60 不及格
// int score = 48;
// 从控制台读取用户输入订单值
Scanner scanner = new Scanner(System.in);
System.out.println("请输入成绩:");
int score = scanner.nextInt();
// 卫操作
// 非法的输入,直接返回
if (score < 0 || score > 100) {
System.out.println("非法输入");
return;// return 方法结束,后面代码不再执行
}
switch (score / 10) {
case 9:
case 10:
System.out.println("优秀");
break;
case 8:
System.out.println("良好");
break;
default:
System.out.println("不及格");
}
}
可以看出,这个题目用switch语句比用if语句简单。
(三)循环
1、分类
while循环、for循环
2、循环的三要素:
(1)循环条件的初始化
(2)循环条件的控制
(3)循环条件的改变
3、while 循环
while 循环属于先判断条件,然后操作;
do-while 判断,先执⾏⼀次,然后在操作;记住:以后的开发要彻底忘记 do-while;
while(i<=100) {
sum+=i;
i++;
}
System.out.println(sum);
while (boolean) {
//可以反复执行
}
2、for循环
for(循环初始化条件 ;循环判断;循环变更条件)
{
循环语句;
}
for循环第一步:循环初始化条件
for循环第二步:循环判断
满足条件:执行循环体内容
不满足条件:退出循环
for循环第三步:循环变更条件,变更完之后再到第二步。
3、while、for循环主要是两类题目:
(1)累加思想(1+2+3+... + 100)
(2)统计思想,数一下符合条件的有多少个(计算1-100以内7的倍数)
4、无限循环
For 的⽆限循环
for(;;) {
System.out.println("你好");
}
While 的⽆限循环
While(true){
System.out.println("你好");
}
注意:
如果不知道循环次数,但是知道循环结束条件的时候⽤ while 循环;例⼦:吸⼏根烟就死了。 如果知道循环次数时候,使⽤ for 循环;例⼦:如果吸 100 根烟就死了。
5、循环控制
continue、break:
continue:跳出本次循环,继续下一次循环
break:跳出离他最近的那层循环
注意:
1、循环结构需要多些⼏次,
2、while ⽤于不确定次数但是知道结束条件的语句,⽽ for 循环⽤于明确知道确定的次数⽽不知道结
束条件的循环。
3、Continue、break 都需要和 if 语句结合。
public void test3()
{
for (int i = 1; i < 10; i++) {
if (i == 3)
{
continue;
}
System.out.println(i);//跳出 3 其余继续输出
}
}
当使⽤ continue 时,执⾏到 continue 时,跳出这⼀次操作,继续执⾏循环中的后⾯的操作。
6.一个练习
猜数游戏(continue、break:)
范例:break 和 continue 的使⽤
//随机生成⼀个 1~100 之间的整数,让顾客去猜
//如果猜⼤了,输出猜⼤了,继续猜
//如果猜⼩了,输出猜⼩了,继续猜
//如果猜对了,输出猜对了,停⽌游戏
public void test99() {
Scanner scanner = new Scanner(System.in);
// between zero (inclusive) and bound (exclusive)
// [0,100) + 1 = [1, 100]
int random = new Random().nextInt(100) + 1;
System.out.println("random: " + random);
// for (;;) {}
while (true) {
System.out.println("猜吧");
int guessNum = scanner.nextInt();
if (guessNum > random) {
System.out.println("太大了");
} else if (guessNum < random) {
System.out.println("太小了");
} else {
System.out.println("恭喜你猜对了");
break;
}
System.out.println("您是否继续这个游戏(若不继续请输入0)");
int flag = scanner.nextInt();
if(flag == 0){
break;
}
}
System.out.println("程序结束");
}
7、双重for循环是日常使用最多的
双重for循环:
外层循环控制行数,数一下有几行就能确定外层循环。
内层循环控制列数,这一行打印多少个,到底要打印多少个要找出和当前行之间的一个关系。
通过题目感受一下
(1)输出
*
**
***
****
*****
@Test
public void test19() {
// i=1 j=1
// i=2 j=2
// i=3 j=3
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
(2)打印正三角形和倒三角形
@Test
public void test12(){
for(int i = 1;i <=6;i++)
{
for (int j = 1; j <= 6 - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println(" ");
}
for(int i = 1;i <=6;i++){
for (int j = 1; j <= i - 1; j++) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (6 - i) + 1; j++) {
System.out.print("*");
}
System.out.println(" ");
}
}