Math类

package test0811;

public class Test03 {

	public static void main(String[] args) {
		/*
		 * Math类:数学应用相关的类
		 * final修饰的类,不能被继承
		 * Math类中的方法都是static方法,因此不必创建Math类的对象就可以直接使用该类的方法
		 * 静态方法的特征:可以用类名直接调用,也可以用对象名调用;静态方法只能访问静态成员变量,不能用this
		 * 常量: PI  E
		 * 方法:
		 * static double ceil(double):返回大于所给参数的最小整数值
		 * static double floor(double):返回不大于所给参数的最大整数值
		 * static int max(int a,int b):返回两个整数中较大的那个
		 * static int min(int a,int b):返回两个整数中较小的那个
		 * static int round(double): 四舍五入取整
		 * static double random():随机返回0-1之间的小数
		 * 
		 * 
		 */
		System.out.println(Math.E);//2.718281828459045
		System.out.println(Math.PI);//3.141592653589793
        System.out.println(Math.ceil(56.32));//57.0
        System.out.println(Math.floor(56.32));//56.0
        System.out.println(Math.max(5, 8));//8
        System.out.println(Math.min(5, 8));//5
        System.out.println(Math.round(5.56));//6 四舍五入
        System.out.println(Math.random());//0.10882297817896036 范围0-1随机数
        System.out.println(Math.sqrt(2));//1.4142135623730951
        
	}

}
package test0811;

public class Test04 {

	public static void main(String[] args) {
		// 抽奖:一等奖一人,二等奖二人,三等奖三人
		String[] arr = { "张三", "李四", "王五", "秀珍", "浅笑", "冬至", "小雪", "夏至", "秋分", "大雪" };
        String str = "";
        
		// 产生三等奖获得者
		System.out.print("三等奖获得者是:");
		for (int i = 1; i <= 3; i++) {
			int ran = (int) Math.floor(Math.random() * 10);
			if(str.indexOf(String.valueOf(ran))>-1){//该数字已经产生过了
				i--;
				continue;
			}else{
				System.out.print(arr[ran]+" ");
				str = str+ran+";";
			}
		}
		// 二等奖
		System.out.println();
		System.out.print("二等奖获得者是:");
		for (int i = 1; i <= 2; i++) {
			int ran = (int) Math.floor(Math.random() * 10);
			if(str.indexOf(String.valueOf(ran))>-1){//该数字已经产生过了
				i--;
				continue;
			}else{
				System.out.print(arr[ran]+" ");
				str = str+ran+";";
			}
		}
		// 一等奖
		System.out.println();
		System.out.print("一等奖获得者是:");
		while(true){
			int ran = (int) Math.floor(Math.random() * 10);
			if(str.indexOf(String.valueOf(ran))>-1){//该数字已经产生过了
				continue;
			}else{
				System.out.print(arr[ran]+" ");
				str = str+ran+";";
			}
			break;
		}
	}

}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值