公式: [a,b] -> (int)(Math.random() * (b - a + 1) +a)
class RandomInt {
public static void main(String[] args){
int value = (int)(Math.random() * 90 + 10);
System.out.println(value);
}
}
switch(表达式){
case 常量1:
语句1:
// break;
case 常量2:
语句2:
// break;
......
case 常量N:
语句N:
// break;
default:
语句:
// break;
}
class SwitchCaseTest {
public static void main(String[] args){
int number = 2;
switch(number){
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("other");
}
}
}
class SwitchCaseTest2 {
public static void main(String[] args){
int score = 78;
switch(score / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("不及格");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("及格");
break;
}
// 更优的解决方案:
switch(score / 60){
case 0:
System.out.println("不及格");
break;
case 1:
System.out.println("及格");
break;
}
}
}
Java 基础(获取随机数, switch-case 结构)
原文:https://www.cnblogs.com/klvchen/p/14216944.html