Java的Math类

  • 直接调用,不用导包
  • Math.random() [0.0,1.0) 随机小数
  • [min,max) Math.random()*(max-min)+min; [0.0,1.0)*4=[0,4)+3=[3,7) [3,7)
  • [min,max] (int)(Math.random()*(max-min+1)+min);
public class Math02{ 
	
	public static void main(String[] args){ 
		System.out.println((int)(Math.random()));
		//[3,7)  //[3,7]
		int i= (int)(Math.random()*(7-3+1)+3);
		System.out.println(i);
		
		System.out.println(Math.round(1.49));
		System.out.println(Math.abs(-1.49));
		System.out.println(Math.sqrt(2));
		
		/*
			随机产生一个a~z之间的小写字母,判断是否是元音  a e i o u
		*/
		
		char ch = (char)(Math.random()*26+'a');
		switch(ch){
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				System.out.println(ch+"是元音字母");
				break;
			default:
				System.out.println(ch+"不是元音字母");
				break;
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值