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;
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7247cb283cbd5c88196c4650faacda98.png#pic_center)