顺序结构
选择结构
- 单选择结构
1.if单选择结构
- 双选择结构
if-else双选择结构
- 多选择结构
if-else if -else多选择结构
switch多值选择结构(要注意case穿透问题,一般在每一个case都要就break语句)
循环结构
while循环语句:先判断在执行
DoWhile循环语句:先执行再判断
public class TestIf {
public static void main(String[] args) {
double d = Math.random();
System.out.println("double d: " + d);
int e = 1 + (int) (d * 6); //[1,6]
System.out.println("&&&&&&&&&&&if语句");
if (e > 3) {
System.out.println("e>3");
} else {
System.out.println("e<=3");
}
System.out.println("*****************下面的例子正好利用case穿透现象解决问题");
char c = 'a';
int rand = (int) (26 * Math.random());
char c2 = (char) (c + rand);
System.out.print("c2 = " + c2 + ",是");
switch (c2) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("元音");
break;
case 'w':
case 'y':
System.out.println("丰元音");
break;
default:
System.out.println("辅音");
}
System.out.println("JDK7.0新特性之switch语句,表达式可以是字符串");
String day = "周三";
switch (day) {
case "周一":
System.out.println("周一");
break;
case "周二":
System.out.println("周二");
break;
case "周三":
System.out.println("周三");
break;
case "周四":
System.out.println("周四");
break;
case "周五":
System.out.println("周五");
break;
default:
System.out.println("周末");
break;
}
System.out.println("###############while循环");
int i = 1;
int sum = 0;
while (i <= 100) {
sum += i;
i++;
}
System.out.println("while循环结构,sum =" + sum);
System.out.println("###############Dowhile循环");
do {
sum += i;
i++;
} while (i <= 100);
System.out.println("DoWhile循环结构,sum =" + sum);
}
}