Math类的常用方法

- 日理万妓 2022-03-28 05:30 475阅读 0赞

Math类的常用方法

1.绝对值:Math.abs(a);
(a可以是int、float、double类型,相对应的返回值也为int、float、double)

  1. public class MathJueDuiZhi {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. double b = scan.nextDouble();
  5. double a = Math.abs(b);
  6. System.out.println(b+"的绝对值是"+a);
  7. }
  8. }

2.求幂(某一个数的n次方):Math.pow(double a,double b);
求a的b次方(b个a相乘)。

  1. public class MathQiuMi {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. System.out.println("输入一个数!");
  5. int num = scan.nextInt();
  6. System.out.println("求它的几次方:");
  7. int a = scan.nextInt();
  8. double b = Math.pow(num, a);
  9. System.out.println(num + "的" + a + "次方的值为:" + b);
  10. }
  11. }

3.e的N次方的值:Math.exp(double a)

  1. public class MathENCiFang {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. System.out.println("输入一个数!");
  5. double a = scan.nextDouble();
  6. double b = Math.exp(a);
  7. System.out.println("e的" + a + "次方是:" + b);
  8. }
  9. }

4.e的N次方-1的值:Math.Math.expm1(double a)

  1. public class MathENCiFang {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. System.out.println("输入一个数!");
  5. double a = scan.nextDouble();
  6. double b = Math.expm1(a);
  7. System.out.println("e的" + a + "次方-1是:" + b);
  8. }
  9. }

5.立方根:Math.cbrt(double a);

  1. public class MathLiFangGen {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. System.out.println("输入一个数!");
  5. double a = scan.nextDouble();
  6. double b = Math.cbrt(a);
  7. System.out.println(a + "的立方根为:" + b);
  8. }
  9. }

6.返回不小于a的最小整数:Math.ceil(double a);

  1. public class MathZuiXiaoZhengShu {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. System.out.println("输入一个数!");
  5. double a = scan.nextDouble();
  6. double b = Math.ceil(a);
  7. System.out.println("不小于" + a + "的最小整数为:" + b);
  8. }
  9. }

7.返回不大于a的最大整数:Math.floor(double a);

  1. public class MathZuiDaZhengShu {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. System.out.println("输入一个数!");
  5. double a = scan.nextDouble();
  6. double b = Math.floor(a);
  7. System.out.println("不大于" + a + "的最大整数为:" + b);
  8. }
  9. }

8.返回等于或小于代数商的最大整数值:Math.floorDiv(a, b);
(a,b可以为long或者int型,a是除数,b是被除数)

  1. public class MathFloorDiv {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. int a = scan.nextInt();
  5. int b = scan.nextInt();
  6. int c = Math.floorDiv(a, b);
  7. System.out.println(a+"/"+b+"的最大整数值是:"+c);
  8. }
  9. }

输入a=12,b=5
输出结果:a/5的最大整数值是:2

9.余数:Math.floorMod(a, b);

  1. public class MathFloorMod {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. int a = scan.nextInt();
  5. int b = scan.nextInt();
  6. int c = Math.floorMod(a, b);
  7. System.out.println(a+"%"+b+"=:"+c);
  8. }
  9. }

输入:a=9,b=5
输出结果:9%5=4

10.两个参数平方值之和的平方根:Math.hypot(double a, double b);

  1. public class MathHypot {
  2. public static void main(String[] args) {
  3. Scanner scan = new Scanner(System.in);
  4. double a = scan.nextDouble();
  5. double b = scan.nextDouble();
  6. double c = Math.hypot(5.6, 6.5);
  7. System.out.println(a+"和"+b+"的平方值之和的平方根为:"+c);
  8. }
  9. }

输入:a=3,b=4
输出结果:3和4的平方值之和的平方根为5

  • 返回较小的数:Math.min(a.b) 如:Math.min(1, 2) =1
    返回较大的数:Math.max(a.b) 如:Math.max(1, 2) = 2
  • 返回0,1之间的一个随机数:Math.random();

  • 余弦函数:Math.cos(a);
  • 反余弦函数:Math.acos(a);
  • 双曲余弦值:Math.cosh(a);
  • 正弦函数:Math.sin(a);
  • 反正弦函数:Math.asin(a);
  • 正切函数:Math.tan(a);
  • 反正切函数:Math.atan(a);
  • 商的反正切函数:Math.atan2(a,b);
  • 两个数字相加:Math.addExact(a,b);
    (a,b可以为int或long型)
  • Math.copySign()方法将第一个参数的标记设置为第二个参数的标记。
  • Math.decrementExact(int a);无溢出运算,具体用法还不清楚。
    Math.getExponent(double/floor a);

发表评论

表情:
评论列表 (有 0 条评论,475人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java Math方法

    Java Math类的常用方法 在 Java 中 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数、对数、平方根和三角函数等。 绝对值 方法: