Math与Random类

来源:互联网 发布:软件测试教程推荐 编辑:程序博客网 时间:2024/05/17 01:18

1.Math 类

表示数学操作,例如:平方根、四舍五入等。

public class MathDemo01{public static void main(String args[]){// Math类中的方法都是静态方法,直接使用“类.方法名称()”的形式调用即可System.out.println("求平方根:" + Math.sqrt(9.0)) ;System.out.println("求两数的最大值:" + Math.max(10,30)) ;System.out.println("求两数的最小值:" + Math.min(10,30)) ;System.out.println("2的3次方:" + Math.pow(2,3)) ;System.out.println("四舍五入:" + Math.round(33.6)) ;}};

2.Random 类

Random 类的主要功能是产生随机数,可以产生一个指定范围的随机数,Random 类是定义在java.util 包中的类。

生成10个随机数字,且数字不大于100

import java.util.Random ;public class RandomDemo01{public static void main(String args[]){Random r = new Random() ;// 实例化Random对象for(int i=0;i<10;i++){System.out.print(r.nextInt(100) + "\t") ;}}};


原创粉丝点击