本文主要内容:讲清 Java 里的条件判断(if/switch)与循环(for/while/do-while),并掌握
break/continue的用法。 适合人群:刚学完变量、运算符,准备写出“能跑起来的小程序”的零基础同学。 阅读收益:看完你能独立写出常见的判断与循环,并避免新手最容易写错的语法。
当你需要“根据条件决定做哪件事”时,就用 if。
true 才会进入对应分支书写格式
if (条件1) {
// 条件1成立
} else if (条件2) {
// 条件2成立
} else {
// 以上都不成立
}案例:成绩等级(含合法性判断)
import java.util.Scanner;
public class Demo01IfElseIf {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入分数:");
int score = sc.nextInt();
if (score < 0 || score > 100) {
System.out.println("分数不合法");
} else if (score >= 90) {
System.out.println("A");
} else if (score >= 80) {
System.out.println("B");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
}
}固定格式,使用场景不多,了解即可
当你手里只有一个变量,并且它只会落在“几个固定值”里(例如 1~7、菜单选项),可以用 switch。
case 后都写 break,避免穿透书写格式
switch (变量) {
case 值1:
// ...
break;
case 值2:
// ...
break;
default:
// 兜底
}案例:输入 1~7 输出“周几”
import java.util.Scanner;
public class Demo02Switch {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入 1~7:");
int day = sc.nextInt();
switch (day) {
case 1:
System.out.println("周一");
break;
case 2:
System.out.println("周二");
break;
case 3:
System.out.println("周三");
break;
case 4:
System.out.println("周四");
break;
case 5:
System.out.println("周五");
break;
case 6:
System.out.println("周六");
break;
case 7:
System.out.println("周日");
break;
default:
System.out.println("输入不合法");
}
}
}当你知道循环要执行多少次(或范围很明确,比如 1~100),优先用 for。
i 当计数器书写格式
for (int i = 起始值; i <= 结束值; i++) {
// 循环体
}案例:计算 1~100 的和
public class Demo03ForSum {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println("sum=" + sum);
}
}当你不确定要循环多少次,但你知道“什么时候该停”(比如:输入合法了就停),用 while。
书写格式
初始化;
while (条件) {
// 循环体
更新;
}案例:一直输入,直到输入正整数
import java.util.Scanner;
public class Demo04WhileInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个正整数:");
int num = sc.nextInt();
while (num <= 0) {
System.out.println("输入不合法,请重新输入正整数:");
num = sc.nextInt();
}
System.out.println("你输入的是:" + num);
}
}使用场景较少,了解即可
do...while 和 while 很像,但它会先执行一次再判断条件,所以“至少执行 1 次”。
书写格式
do {
// 循环体
} while (条件);案例:至少输入一次密码(正确才结束)
import java.util.Scanner;
public class Demo05DoWhilePassword {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String pwd;
do {
System.out.println("请输入密码:");
pwd = sc.next();
} while (!pwd.equals("123456"));
System.out.println("登录成功");
}
}break 可以让你在循环还没跑完时,直接“提前退出”。
书写格式
for (...) {
if (条件) {
break;
}
}案例:找到第一个能被 7 整除的数
public class Demo06Break {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0) {
System.out.println("第一个能被 7 整除的数:" + i);
break;
}
}
}
}continue 的意思是:本轮循环“后面的代码别执行了”,直接进入下一轮。
书写格式
for (...) {
if (条件) {
continue;
}
// 本轮真正要执行的逻辑
}案例:打印 1~20,跳过 3 的倍数
public class Demo07Continue {
public static void main(String[] args) {
for (int i = 1; i <= 20; i++) {
if (i % 3 == 0) {
continue;
}
System.out.println(i);
}
}
}无限循环就是“循环条件永远为真”,程序会一直跑下去。
while(true) 或 for(;;)break(满足退出条件时跳出),否则程序不会结束书写格式
while (true) {
// 循环体
if (退出条件) {
break;
}
}
for (;;) {
// 循环体
if (退出条件) {
break;
}
}案例:不断输入数字,输入 0 结束
import java.util.Scanner;
public class Demo08InfiniteLoop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入一个整数(输入0结束):");
int num = sc.nextInt();
if (num == 0) {
System.out.println("结束");
break;
}
System.out.println("你输入的是:" + num);
}
}
}Random 用来生成随机数,常见用途是:做随机验证码、抽奖、小游戏等。
nextInt(n) 生成的范围是:0 ~ n-11 ~ n,可以写成:nextInt(n) + 1书写格式
import java.util.Random;
Random r = new Random();
int num = r.nextInt(上限); // 0 ~ 上限-1案例:生成 1~100 的随机数
import java.util.Random;
public class Demo09Random {
public static void main(String[] args) {
Random r = new Random();
int num = r.nextInt(100) + 1;
System.out.println(num);
}
}回顾一下:先用例子把代码跑起来,再回头记住语法要点。条件判断用
if/else/else if,固定值分支用switch。重复执行用循环,并用break/continue控制流程。