package Gaojiyuyan; /* java.util.Math类是数学工具类,提供大量的静态方法,完成数学运算操作 public static double abs(double num):获取绝对值,有多种重载 public static double ceil(double num):向上取整 public static double floor(double num):向下取整 public static Long round(double num):四舍五入 Math.PI代表近似的圆周率常量 */ public class MathLei { public static void main(String[] args) { //获取绝对值 System.out.println("获取绝对值:"); System.out.println(Math.abs(0)); System.out.println(Math.abs(-222)); System.out.println(Math.abs(222)); System.out.println("**************************"); //向上取整 System.out.println("向上取整:"); System.out.println(Math.ceil(3.39)); System.out.println(Math.ceil(-3.39)); System.out.println(Math.ceil(3.3)); System.out.println("**************************"); //向下取整 System.out.println("向上取整:"); System.out.println(Math.floor(1.1)); System.out.println(Math.floor(1.9)); System.out.println(Math.floor(-1.1)); System.out.println("**************************"); //四舍五入 System.out.println("四舍五入:"); System.out.println(Math.round(20.5)); System.out.println(Math.round(20.4)); System.out.println(Math.round(-20.4)); System.out.println("**************************"); System.out.println("圆周率:"); System.out.println(Math.PI); System.out.println("**************************"); //练习:计算在-10.8到5.9之间,绝对值大于6或小于2.1的整数有多少个? /* 1,有范围,用for循环 2,起点位置-10.8应该转换成-10, 两种方法:Math.ceil方法向上取整。或强转为int。舍弃小数 3.步进表达式num++ 4.绝对值:Math.abs 5.发现一个数字,需要让计数器++进行统计 */ int count = 0;//符合要求的数量 double min = -10.8; double max = 5.9; /* for(int i=(int)min;i<max;i++){ int abs=Math.abs(i); if(abs>6||abs<2.1){ System.out.println(i); count++; } } */ for (int i = (int) min; i < max; i++) { double abs=Math.abs(i); System.out.println(Math.ceil(abs)); if(abs>6||abs<2.1){ System.out.println(i); count++; } } } }
Math类
最新推荐文章于 2024-09-10 10:21:38 发布