Java Flow Control Statement 流程控制语句
用来控制程序的执行流程
Loop differentiation 循环区别
for | 如果已知循环多少次使用for,控制循环只能在循环中{}中使用,先判断后执行 |
while | 如不清楚执行多少循环使用while,控制循环后还可以使用,先判断后执行 |
do_while | 首次执行后判断 |
Jump loop statement 跳转循环语句
break | 用于结束当前循环语句或结束所在switch分支执行 |
continue | 用于跳出当前循环进入下一个循环程序 |
sequence 顺序结构
默认程序执行顺序
Branch structure 分支结构
If.case
Scanner sc = new Scanner(System.in);
int heartbeat = sc.nextInt();
if (heartbeat < 60 || heartbeat > 100) {
System.out.println(("您的心跳数据是:" + heartbeat + "您需要进一步检查"));
}
System.out.println(("检查结果" + heartbeat + "您的身体ok"));
Scanner sc = new Scanner(System.in);
System.out.println(("请输入红包金额:"));
double money = sc.nextDouble();
double money_one = 1000;
if (money_one >= money) {
System.out.println(("发送成功"));
}else {
System.out.println(("no"));
}
Scanner sc = new Scanner(System.in);
System.out.println("请数");
int jx = sc.nextInt();
if (jx >= 0 && jx < 60) {
System.out.println(("C:1000"));
} else if (jx >= 60 && jx < 80) {
System.out.println(("B:2000"));
} else if (jx >= 80 && jx <= 100) {
System.out.println("A:3000");
} else {
System.out.println("最高100");
}
switch.case
适合做值匹配选择结构格式清晰
Scanner sc = new Scanner(System.in);
System.out.println(("请输入:"));
String Phone = sc.next();
switch (Phone) {
case "苹果":
System.out.println(("IPhone" + "12ProMax" + "256G"));
break;
case "华为":
System.out.println(("HuaWei" + "40" + "256G"));
case "小米":
System.out.println(("XIAOMi" + "20" + "64G"));
default:
System.out.println(("无数据"));
}
Loop Structure 循环结构
for.case
Scanner sc =new Scanner(System.in);
System.out.println(("请输入:"));
String HelloWorld_tect = sc.next();
for (int i = 0; i < 3; i++){
System.out.println((HelloWorld_tect));
}
while.case
int i = 0;
while (i < 3) {
System.out.println(("he"));
i++;
}
Scanner sc = new Scanner(System.in);
System.out.println(("INPUT1"));
double w = sc.nextDouble();
double D = 8848860;
int counter = 0;
while (w < D){
w *= 2;
counter++;
}
System.out.println("INPUT " + counter);
System.out.println(("" + w));
do-while.case
Scanner sc = new Scanner(System.in);
System.out.println(("INPUT1"));
double w = sc.nextDouble();
int conter = 0;
do {
System.out.println(w);
w++;
conter++;
} while (w < 20);
{
}
System.out.println("???" + (conter));
endless loop case 死循环案例
Scanner sc = new Scanner(System.in);
int UserPassword = 520;
while (true) {
System.out.println(("INPUT USER PASSWORD"));
int Password = sc.nextInt();
if (Password == UserPassword) {
System.out.println(("CORRect"));
break;
} else {
System.out.println(("Password error INPUT"));
}
nesting loop case 嵌套循环
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 3; j++) {
System.out.println(("A"));
}
System.out.println(("---------"));
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 5; j++) {
System.out.print(("*"));
}
System.out.println();
continue case 案例
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println(("INPUT NUMBER" + i));