0
点赞
收藏
分享

微信扫一扫

java数学库


文章目录

  • ​​一、最大值​​
  • ​​二、最小值​​
  • ​​三、开平方​​
  • ​​四、绝对值​​
  • ​​五、随机数​​

一、最大值

查找x和 y 的最大值:Math.max(x,y)

package test6;

public class test1 {
public static void main(String [] args)
{
System.out.println(Math.max(5, 10));
}
}

运行:
java数学库_最小值

二、最小值

用于查找x 和y的最小值:Math.min(x,y)
举例:

package test6;

public class test2 {
public static void main(String [] args)
{
System.out.println(Math.min(2,5));
}
}

运行:
java数学库_最小值_02

三、开平方

返回x 的平方根:Math.sqrt(x)

举例如下:

package test6;

public class test3 {
public static void main(String [] args)
{
System.out.println(Math.sqrt(9));
}
}

运行:
java数学库_随机数_03

四、绝对值

回x的绝对(正)值:Math.abs(x)

比如求-5的绝对值:

package test6;

public class test4 {
public static void main(String [] args)
{
System.out.println(Math.abs(-5));
}
}

运行:
java数学库_随机数_04

五、随机数

Math.random() 返回 0.0(含)和 1.0(不含)之间的随机数:

package test6;

public class test5 {
public static void main(String [] args)
{
System.out.println(Math.random());
}
}

运行:
java数学库_开发语言_05
为了更好地控制随机数,例如您只想要一个 0 到 100 之间的随机数,您可以使用以下公式:

package test6;

public class test6 {
public static void main(String [] args)
{
int a = (int)(Math.random() * 101); // 0100
System.out.println(a);
}
}

运行:
java数学库_最小值_06
这些是比较常见的数学库,一定要掌握。


举报

相关推荐

0 条评论