栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 常用api之Math常用的八个方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 常用api之Math常用的八个方法

math类的常用的八个方法
    1.public static int abs(int a ):返回参数的绝对值 
    2.public static double ceil(double a ):返回大于或等于参数的最小double值,等于一个整数
    3.public static double floor(double a ):返回大于或等于参数的最大double值,等于一个整数
    4.public static int round(float a ):按照四舍五入返回最接近参数的整数
    5.public static int max(int a,int b):返回两个int值中的较大值
    6.public static int min(int a ):返回两个int值中的较小值
    7.public static double pow(double a,double b):返回a的b次幂的值
    8.public static double random():返回值为double的正值【0.0,1.0】,能够取得到0.0,但是取不到1.0 (还有取整和取值到1)

以下是这八个方法的范例,其实看一遍,运行一遍就可以明白了

package math;

public class mathdemo {
	
public static void main(String[] args) {
		// TODO 自动生成的方法存根
//	1.public static int abs(int a ):返回参数的绝对值 
	System.out.println(Math.abs(88));
	System.out.println(Math.abs(-88));
	System.out.println("-----------------------");
//	2.public static double ceil(double a ):返回大于或等于参数的最小double值,等于一个整数
	System.out.println(Math.ceil(12.34));
	System.out.println(Math.ceil(12.56));
	System.out.println("-----------------------");
//  3.public static double floor(double a ):返回大于或等于参数的最大double值,等于一个整数
	System.out.println(Math.floor(12.34));
	System.out.println(Math.floor(12.56));
	System.out.println("-----------------------");
//	4.public static int round(float a ):按照四舍五入返回最接近参数的整数
	System.out.println(Math.round(12.34F));
	System.out.println(Math.round(12.56F));
	System.out.println("-----------------------");
//	5.public static int max(int a,int b):返回两个int值中的较大值
	System.out.println(Math.max(12,13));
	System.out.println(Math.max(13,45));
	System.out.println("-----------------------");
//	6.public static int min(int a ):返回两个int值中的较小值
	System.out.println(Math.min(12,13));
	System.out.println(Math.min(13,45));
	System.out.println("-----------------------");
//	7.public static double pow(double a,double b):返回a的b次幂的值
	System.out.println(Math.pow(2.0,2.0));
	System.out.println(Math.pow(3.0,3.0));
	System.out.println("-----------------------");
//	8.public static double random():返回值为double的正值【0.0,1.0】,能够取得到0.0,但是取不到1.0
	System.out.println(Math.random());
	System.out.println(Math.random()*100);
	System.out.println((int)(Math.random()*1000));//取整   强制转换
	System.out.println((int)(Math.random()*1000)+1);//取值范围就是 0.0到1.0之间了,包括这俩
	System.out.println("-----------------------");
	

	}

}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979314.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号