多条件判断,判断条件是等值判断时,可以用switch
比如:
变量level=0时,输出“非会员”
变量level=1时,输出“打9折”
变量level=2时,输出“打8折”
变量level=3时,输出“打7折”
变量level=4时,输出“打6折”
变量level为其他数时,输出“打6折”
import java.util.Random; public class Test2 { public static void main(String[] args){ Random random = new Random(); int level = random.nextInt(10);//生成[0,9]的随机整数 //case后面必须跟常量 //break不可省略 switch (level){ case 0: System.out.println("非会员"); break; case 1: System.out.println("打9折"); break; case 2: System.out.println("打8折"); break; case 3: System.out.println("打7折"); break; case 4: System.out.println("打6折"); break; default://default可以省略,编写在末尾 System.out.println("打5折"); break; } } }