Math 和 Integer对数的操作方法和

xiaoxiao2021-02-28  60

// Number类 Integer a=5;

a=a+5;

System.out.println(a);  

// Math类 final double PI=3.14; System.out.println(Math.sin(PI/2));

System.out.println(Math.cos(PI/2)); System.out.println(Math.tan(PI/2)); System.out.println(Math.PI);   

// 数据.数据类型Value 返回数据类型方法

 // compareTo 比较相等方法 等于返回0 否则返回-1

Integer x=5; System.out.println(x.doubleValue());

System.out.println(x.compareTo(10));

 

// equals()返回true与false,判断两数是否相等  Integer a=5,b=5;

 System.out.println(a.equals(5)+" "+a.equals(b));   

// toString 方法,一种返回Integer的string对象,一种返回int的string对象 Integer a=5; System.out.println(a.toString()); System.out.println(Integer.toString(45));

 

// psrseInt返回字符串的数值对象 int x=Integer.parseInt("456"); double xx=Double.parseDouble("5"); System.out.println(x); System.out.println(xx);

  

// Math的abs取绝对值 int a=-45; Integer b=-56; System.out.println(Math.abs(a)); System.out.println(Math.abs(b));

  

// min与max的Math方法 System.out.println(Math.min(12, 6)); System.out.println(Math.max(5,    * 9)); //pow 数的乘方  sqrt 数扥平方根 System.out.println(Math.pow(12, 2.3)); System.out.println(Math.sqrt(6));

 

//random返回一个随机数

 System.out.println("值的范围在0.0到0.99之间:"+Math.random());  System.out.println("取0到8的随机数:"+(int)(Math.random()*9));      //*9时,因为0.999*9=8....,所以想取8就得乘8+1  System.out.println("取4到8的随机数:"+(int)(Math.random()*5+4));  //加4是为了把数的最小值定义为4,加上*5的话就随机+了01234

转载请注明原文地址: https://www.6miu.com/read-53448.html

最新回复(0)