天天看点

Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法

Math类概述和类中基本使用方法

  • 什么是Math类
  • Math类中常用成员方法的概述
  • Math类中常用成员的使用方法

什么是Math类

Math类就是包含用于执行基本数学运算的方法,比如初等指数,对数,平方根和三角函数

Math类中常用成员方法的概述

Math.abs(int a); 返回给定值的绝对值。

Math.ceil(double a); 返回最接近(向上取值)double 值的整数值,该值大于等于参数,并等于某个整数

Math.floor(double a); 返回最接近(向下取值)double 值的,该值小于等于参数,并等于某个整数。

Math.max(int a,int b); 返回两个 int 值中较大的一个。

Math.min(int a,int b); 返回两个 int 值中较小的一个。

Math.pow(int a,int b);返回第一个参数的第二个参数次幂的值。

Math.randow();返回一个随机带正号的 double 值,该值大于等于 0.0 且小于 1.0。

Math.round(double a);返回最接近参数的 long类型值(如果参数是float类型,则返回最接近参数的int类型值)

Math.sqrt(double a);返回正确舍入的 double 值的正平方根。

Math类中常用成员的使用方法

Math.abs( )的使用

描述: 请输出给定值得绝对值, -10,20,0,-11.4

public class absoluteValue {

	public static void main(String[] args) {
		int a = -10;    
		int b = 20;
		int c = 0;
		double d = -11.4;
		
		System.out.println(a+"的绝对值是:"+Math.abs(a));
		System.out.println("--------------------------------");
		System.out.println(b+"的绝对值是:"+Math.abs(b));
		System.out.println("--------------------------------");
		System.out.println(c+"的绝对值是:"+Math.abs(c));
		System.out.println("--------------------------------");
		System.out.println(d+"的绝对值是:"+Math.abs(d));
		//注意:Math.abs() 的参数不能是 boolean和char和String

	}
           
Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法

Math.ceil( ) 和 Math.floor( )的使用

描述:给定一个值 11.5 请返回最接近它的一个整数的值

public class CeilAndFloor {

	public static void main(String[] args) {
		double a = -11.8;
		System.out.println(Math.ceil(a));
		System.out.println("------------------------");
		System.out.println(Math.floor(a));
		//注意:如果输入的数是一个整数,则返回的ceil和floor值等于输入的这个整数
	}

}
           
Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法

Math.max( ) 和 Math.min( )的使用

描述:输入两个整数 8 和 20 ,请返回这两个数中的最大值和最小值

public class MaxAndMin {

	public static void main(String[] args) {
		int a = 8 , b = 20;
		System.out.println("最大值为:"+Math.max(a, b));
		System.out.println("-----------------------");
		System.out.println("最小值为:"+Math.min(a, b));
	}

}
           
Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法

Math.pow( )的使用

描述:请输出2的3次方

public class Power {

	public static void main(String[] args) {
		int a = 2;
		int b = 3;
		System.out.println("2的3次方的值="+Math.pow(a, b));
	}

}
           
Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法

Math.random( )的使用

描述:请产生一组随机数,存储在数组中,数组长度为5,并打印出来

public class Randow {

	public static void main(String[] args) {
		double[] arr = new double[5];
		for(int i = 0; i < arr.length; i++) {
			arr[i] = Math.random(); 
			System.out.println(arr[i]);
		}
	}
}
           
Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法

Math.round( )的使用

描述:返回最接近2.5的long值,和2.4的int值

public class Round {

	public static void main(String[] args) {
		double a = 2.5;
		float b = 2.4f;
		
		System.out.println(Math.round(a)); 
		System.out.println("-------------------");
		System.out.println(Math.round(b));
		//注意:返回值要进行四舍五入
	}
}
           
Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法

Math.sqrt( )的使用

描述:请计算出8,88,888,8888的平方根

public class Sqrt {

	public static void main(String[] args) {

		int a = 8, b = 88, c = 888, d = 8888;

		System.out.println("8的平方根="+Math.sqrt(a));
		System.out.println("------------------");
		System.out.println("88的平方根="+Math.sqrt(b));
		System.out.println("------------------");
		System.out.println("888的平方根="+Math.sqrt(c));
		System.out.println("------------------");
		System.out.println("8888的平方根="+Math.sqrt(d));

	}
}
           
Math类概述和类中基本使用方法什么是Math类Math类中常用成员方法的概述Math类中常用成员的使用方法