程序流程控制
switch分支这注意事项
1.表达式类型只能是byte,short,int,char,JDK5开始支持枚举,JDK7开始支持String,
不支持double,float,long。
switch穿透性:
不写break遇到case一直往下走,直到遇到break
int month=7;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
System.out.println("是31天");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("是30天");
break;
case 2:
System.out.println("闰年29天,非闰年28天");
break;
default:
System.out.println("数据有误");
Random生成随机数
Random r=new Random();
int data=r.nextInt(10); //0~9的随机数,不包含10
System.out.print(data+"\t");
int data=r.nextInt(11)+10; //生成10~20:先生成0~10的随机数,再加10
System.out.print(data+"\t");
//猜数
Random r=new Random();
int num=r.nextInt(100)+1;
System.out.println("请输入猜测数字:");
Scanner sc=new Scanner(System.in);
while(true){
int guessnum=sc.nextInt();
if(guessnum<num) System.out.println("猜小了");
else if(guessnum>num) System.out.println("猜大了");
else {
System.out.println("猜对,随机数是:" + guessnum);
break;
}
}
标签:case,10,int,Day2,System,程序流程,mt,println,out
From: https://www.cnblogs.com/mt-nika/p/17038462.html