java===Math類

package 經常使用類.Math;

import java.util.Random;

/**Math提供了操做數學運算的方法,都是靜態的。
 * 經常使用方法:Math.abs();返回絕對值
 *        Math.ceil();返回大於參數的最小整數
 *        Math.floor();返回小於參數的最大整數
 *        Math.round();返回四捨五入的整數
 *        Math.random();返回一個僞隨機數double類型,在0.0到1.0之間;注意要想有更多操做建議直接使用Random類實例來調用nextInt方法獲取隨機數
 *        Math.pow(10,2);返回10的2次冪
 *        Math.max(a,b);返回較大的一個數
 *        */
public class MathDemo {

    public static void main(String[] args) {
        //demo_1(12.56);
        demo_2();
//        double d1=Math.max(12.13, 14.15);
//        System.out.println(d1);
//        double d2 =Math.pow(10, 2);//10的2次冪;
//        System.out.println(d2);

    }

    private static void demo_2() {
        Random r1 = new Random();
        for (int i = 0; ; i++) {
            double d1 =Math.ceil(Math.random()*6);
            //double d2 =(int)(r1.nextDouble()*6+1);
            //int d2 =r1.nextInt()*6+1;
            System.out.println(d1);
            if(d1==1)
                return;
            
        }
    }

    public static void demo_1(double d) {
        double d1 = Math.ceil(d);
        double d2 = Math.floor(d);
        double d3 = Math.round(d);
        System.out.println(d1);
        System.out.println(d2);
        System.out.println(d3);
    }

}
相關文章
相關標籤/搜索