switch
default的位置和省略
- 位置 :default不一定是写在最下面的,我们可以写在任意位置。只不过习惯会写在最下面
- 省略 : default可以省略,语法不会有问题,但是不建议省略。(表达式都不匹配,且没有default,不会输出东西)
case穿透
就是语句体中没有写break导致的执行流程:
首先还是会拿着小括号中表达式的值跟下面每一个case进行匹配。
如果匹配上了,就会执行对应的语句体,如果此时发现了break,那么结束整个switch语句。
如果没有发现break,那么程序会继续执行下一个case的语句体,一直遇到break或者右大括号为止。
使用场景:
如果多个case的语句体重复了,那么我们考虑利用case穿透去简化代码。
JDK12 switch新特性
直接用 -> {}代替:,不需要break,不会发生case穿透,{}内只有一个语句时可以省略{}
switch和if第三种格式(if-else if-else)各自的使用场景
if的第三种格式:一般用于对范围的判断
switch:把有限个数据一一列举出来,让我们任选其一
练习
package switchDemo;
import java.util.Scanner;
public class switchDemo2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("choose:");
int choose = sc.nextInt();
switch (choose){
case 1 -> System.out.println("check!");
case 2 -> System.out.println("book!");
case 3 -> System.out.println("change!");
//case 4 -> System.out.println("exit!");
default -> System.out.println("exit!");
}
}
}
循环结构
For
练习:求和
package loopDemo;
public class loopDemo1 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i;
}
System.out.println(sum);
}
}
扩展小点:
- 求和的变量不能定义在循环的里面,因为变量只在所属的大括号中有效
- 如果我们把变量定义在循环的里面,那么当前变量只能在本次循环中有效。
- 当本次循环结束之后,变量就会从内存中消失。第二次循环开始的时候,又会重新定义一个新的变量。
- 结论:如果以后我们要写累加求和的变量。可以把变量定义在循环的外面。
- 快速for (100.fori直接生成一个for)
package loopDemo;
public class loopDemo1 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) sum += i;
}
System.out.println(sum);
}
}
package loopDemo;
import java.util.Scanner;
public class loopDemo1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("start:");
int start = sc.nextInt();
System.out.println("end:");
int end = sc.nextInt();
int count = 0;
for (int i = start; i <= end; i++) {
if (i % 3 == 0 && i % 5 ==0) count++;
}
System.out.println(count);
}
}
While
For和while的对比
但是,for的初始化语句一样可以写在for外面,此时,两种都相同
练习
package loopDemo;
public class loopDemo2 {
public static void main(String[] args) {
double paper = 0.1;
int count = 0;
while (paper <= 8844430){
paper *= 2;
count++;
}
System.out.println(count);
}
}
package loopDemo;
public class loopDemo3 {
public static void main(String[] args) {
int x = 12321;
int temp = x;
int num = 0;
while (x != 0){
int ge = x % 10;
num = num * 10 + ge;
x /= 10;
}
System.out.println(num == temp);
}
}
package loopDemo;
public class loopDemo4 {
public static void main(String[] args) {
int a = 10, b = 3, shang = 0;
while (a >= b){
a -= b;
shang ++;
}
System.out.println("商:" + shang);
System.out.println("余数:" + a);
}
}
Do...while
无限循环 多用while
跳过本次循环 continue
结束整个循环 break
练习
package demo1;
public class test1 {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7 ) {
System.out.println("pass");
continue;
}
System.out.println(i);
}
}
}
package demo1;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("input number > 2:");
int x = sc.nextInt();
for (int i = 1; i <= x; i++) {
if (i * i == x){
System.out.println(i);
break;
} else if (i * i > x) {
System.out.println(i - 1);
break;
}
}
}
}
优化:只需判断x的平方根内是否存在数字被x整除,如果有则不是质数。
获取随机数
范围从这个范围一定是从0开始的。到这个数-1结束
口诀:包头不包尾,包左不包右
用来生成任意数到任意数之间的随机数7~15
1.让这个范围头尾都减去一个值,让这个范围从0开始 -7 0~8
2.尾巴+1 8+1=9
3.最终的结果,再加上第一步减去的值。