1、switch语句:
Scanner scanner = new Scanner(System.in);
System.out.println("请输入水果名:");
String fruit = scanner.next();
switch (fruit) {
case "苹果":
case "小苹果":
System.out.println("苹果、小苹果19元/斤");
break;
case "香蕉":
System.out.println("香蕉8元/斤");
break;
case "葡萄":
System.out.println("葡萄39元/斤");
break;
default:
System.out.println("无此水果");
}
1.1、Switch语句细节:
1、表达式数据类型与case后面常量类型一致,或者是可以自动转换的类型。
2、Switch中返回值只能是:byte、short、int、char、enum、String。
3、case里面的值必须是常量,不能是变量。
4、break是用来跳出循环的,不写会全部执行。
2、for循环:
// 三种特殊写法:
int i = 0;
for ( ; i<= 10 ; ) {
System.out.println("hello");
i++;
}
System.out.println(i); // 11
for(;;) { // 死循环
System.out.println("hello");
}
int count = 4;
for (int i1 = 0,j = 0; i < count; i++, j+=2) {
System.out.println("i" + i + "j" + j);
}
3、while循环、do while循环:
int s = 1;
while (s <= 100) {
if (s % 3 == 0) {
System.out.println(i);
}
i++;
}
int n = 1;
do {
System.out.println(n);
n++;
} while (i <= 10);