目录
1.1 switch-case
1.2 循环结构
1.2.1 for循环
1.2.2 while循环
1.2.3 do-while循环
1.3 小练习
1.3.1 打印菱形:
1.3.2 九九乘法表
1.3.3 100以内质数(笔试题)
1.1 switch-case
switch(表达式){
case 常量1:
执行语句1;
//break;
case 常量2:
执行语句2;
//break;
...
default:
执行语句n;
//break;
}
- 根据switch表达式中的值,依次匹配各个case中的常量,一旦匹配成功,则进入相应case结构中,调用其执行语句,然后继续执行下面所有的语句,除非遇到break或执行结束跳出switch结构。
- switch中的表达式,只能是一下6种数据类型之一:byte、short、chat、int、枚举类型、String类型
- case之后只能声明常量,不能声明范围
- default为可选结构,而且位置灵活
- 凡是可以用switch-case的结构,都可以用if-else,反之不行
- 若既能使用if-else,且又能使用switch-case(switch表达式分支情况不多),优先使用switch-case,效率较高
例题1:对学生成绩大于60分的,输出“合格”,低于60分的输出“不合格”。
Scanner scan = new Scanner(System.in);
System.out.println("Please input the score: ");
int score = scan.nextInt();
switch(score/10) {
case 6:
System.out.println("合格");
break;
case 7:
System.out.println("合格");
break;
case 8:
System.out.println("合格");
break;
case 9:
System.out.println("合格");
break;
case 10:
System.out.println("合格");
break;
default:
System.out.println("不合格");
}
- 若结构中多条case的结果相同,可以考虑合并,对上题代码改动如下:
Scanner scan = new Scanner(System.in); System.out.println("Please input the score: "); int score = scan.nextInt(); switch(score/10) { case 6: case 7: case 8: case 9: case 10: System.out.println("合格"); break; default: System.out.println("不合格"); } - 更优解法
Scanner scan = new Scanner(System.in); System.out.println("Please input the score: "); int score = scan.nextInt(); switch(score/60) { case 0: System.out.println("不合格"); break; case 1: System.out.println("合格"); break; }
例题2:从键盘上输入2021年的“month”和“day”,输出该日期为2021年的第几天。
Scanner scan = new Scanner(System.in);
System.out.println("Please input the month and day: ");
int month = scan.nextInt();
int day = scan.nextInt();
int sum = 0;
switch (month) {
case 12:
sum += 30;
case 11:
sum += 31;
case 10:
sum += 30;
//......
case 2:
sum += 31;
case 1:
sum += day;
}
System.out.println(sum);
1.2 循环结构
- 在某些条件满足的情况下,反复执行特定代码
- 分类
- for循环
- while循环
- do-while循环
- 四要素
- 初始化条件
- 循环条件(boolean型)
- 循环体
- 迭代条件
- 循环嵌套一般不会超过三层,否则代码繁琐
1.2.1 for循环
- 结构
for(初始条件;循环条件;迭代条件){
循环体
}
1.2.2 while循环
- 结构
初始化条件;
while(循环条件){
循环体;
迭代条件;
} - 注意迭代条件,避免死循环
1.2.3 do-while循环
- 结构
初始化条件;
do{
循环体;
迭代条件;
}while(循环条件) - 与while循环相比,do-while循环无论满不满足循环条件,至少会执行一次循环体
1.3 小练习
1.3.1 打印菱形:
for(int i = 1; i <= 4; i ++) {
for(int j = 0; j < 4 - i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*i-1; j++) {
System.out.print('*');
}
System.out.println();
}
for(int i = 0; i < 3; i ++) {
for(int j = 0; j <= i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*(3-i)-1; j++) {
System.out.print('*');
}
System.out.println();
}
1.3.2 九九乘法表
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("" + i + "*" + j + '=' + i * j + " ") ;
}
System.out.println();
}
- for循环
- while循环
- do-while循环
- 初始化条件
- 循环条件(boolean型)
- 循环体
- 迭代条件
- 结构
for(初始条件;循环条件;迭代条件){ 循环体 }
1.2.2 while循环
- 结构
初始化条件;
while(循环条件){
循环体;
迭代条件;
} - 注意迭代条件,避免死循环
1.2.3 do-while循环
- 结构
初始化条件;
do{
循环体;
迭代条件;
}while(循环条件) - 与while循环相比,do-while循环无论满不满足循环条件,至少会执行一次循环体
1.3 小练习
1.3.1 打印菱形:
for(int i = 1; i <= 4; i ++) {
for(int j = 0; j < 4 - i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*i-1; j++) {
System.out.print('*');
}
System.out.println();
}
for(int i = 0; i < 3; i ++) {
for(int j = 0; j <= i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*(3-i)-1; j++) {
System.out.print('*');
}
System.out.println();
}
1.3.2 九九乘法表
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("" + i + "*" + j + '=' + i * j + " ") ;
}
System.out.println();
}
初始化条件;
while(循环条件){
循环体;
迭代条件;
} - 结构
初始化条件; do{ 循环体; 迭代条件; }while(循环条件) - 与while循环相比,do-while循环无论满不满足循环条件,至少会执行一次循环体
1.3 小练习
1.3.1 打印菱形:
for(int i = 1; i <= 4; i ++) {
for(int j = 0; j < 4 - i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*i-1; j++) {
System.out.print('*');
}
System.out.println();
}
for(int i = 0; i < 3; i ++) {
for(int j = 0; j <= i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*(3-i)-1; j++) {
System.out.print('*');
}
System.out.println();
}
1.3.2 九九乘法表
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("" + i + "*" + j + '=' + i * j + " ") ;
}
System.out.println();
}
for(int i = 1; i <= 4; i ++) {
for(int j = 0; j < 4 - i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*i-1; j++) {
System.out.print('*');
}
System.out.println();
}
for(int i = 0; i < 3; i ++) {
for(int j = 0; j <= i; j++) {
System.out.print(' ');
}
for(int j = 0; j < 2*(3-i)-1; j++) {
System.out.print('*');
}
System.out.println();
}
1.3.2 九九乘法表
for(int i = 1; i <= 9; i++) {
for(int j = 1; j <= i; j++) {
System.out.print("" + i + "*" + j + '=' + i * j + " ") ;
}
System.out.println();
}
1.3.3 100以内质数(笔试题)
质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数(规定1既不是质数也不是合数)。
boolean isFlag = true;
for(int i = 2; i < 100; i++) {
for(int j = 2; j <= Math.sqrt(i); j++) {
if(i % j == 0) {
isFlag = false;
break;
}
}
if(isFlag) {
System.out.print("" + i + " ");
}
isFlag = true;
}
System.out.println();
质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数(规定1既不是质数也不是合数)。



