1. Math class overview
The math class contains methods for performing basic mathematical operations, such as elementary exponents, logarithms, square roots, and trigonometric functions.
2. member variables and member methods (common)
(1) member variables
1 Public Static Final Double Pi
1 Public Static Final Double E
(2) Member method
1 Public Static intAbsinta): absolute value2 Public Static DoubleCeil (Doublea): Rounding up3 Public Static DoubleFloorDoublea): Rounding down4 Public Static intMaxintAintb): Max (min self-study)5 Public Static DoublePowDoubleADoubleb): B Power of a6 Public Static DoubleRandom (): stochastic number [0.0,1.0), 0.0 <= x < 1.07 Public Static intRoundfloata) rounding (self-study with double argument)8 Public Static DoublesqrtDoubleA): Positive square root
3. Demo case
1 Packagecn.itcast_01;2 3 /*4 * Math: A class for mathematical operations. 5 * Member variables:6 * public static final double PI7 * public static final double E8 * Member Method:9 * public static int abs (int a): absolute valueTen * public static double ceil (double A): Rounding up One * public static double floor (double a): Rounding down A * public static int max (int a,int b): Max (min self-study) - * public static Double pow (double a,double b): Power of B of a - * public static Double random (): random number [0.0,1.0] the * public static int round (float a) rounded (self-study with double argument) - * public static double sqrt (double A): positive square root - */ - Public classMathdemo { + Public Static voidMain (string[] args) { - //Public static final double PI +System.out.println ("PI:" +Math.PI); A //Public static final double E atSystem.out.println ("E:" +MATH.E); -System.out.println ("--------------"); - - //public static int abs (int a): absolute value -System.out.println ("ABS:" + Math.Abs (10)); -System.out.println ("ABS:" + math.abs (-10)); inSystem.out.println ("--------------"); - to //Public static Double Ceil (double A): Rounding up +System.out.println ("Ceil:" + Math.ceil (12.34)); -System.out.println ("Ceil:" + Math.ceil (12.56)); theSystem.out.println ("--------------"); * $ //Public static Double floor (double a): Rounding downPanax NotoginsengSystem.out.println ("Floor:" + Math.floor (12.34)); -System.out.println ("Floor:" + Math.floor (12.56)); theSystem.out.println ("--------------"); + A //public static int max (int a,int b): Maximum value theSystem.out.println ("Max:" + Math.max (12, 23)); + //requirement: I want to get the maximum value in three data - //nested calls to methods $System.out.println ("Max:" + Math.max (Math.max (12, 23), 18)); $ //requirement: I want to get the maximum value in four data -System.out.println ("Max:" -+ Math.max (Math.max), Math.max (34, 56))); theSystem.out.println ("--------------"); - Wuyi //Public static Double pow (double a,double b): Power of B of a theSystem.out.println ("POW:" + Math.pow (2, 3)); -System.out.println ("--------------"); Wu - //Public static Double random (): random number [0.0,1.0] AboutSystem.out.println ("Random:" +math.random ()); $ //get a random number between 1-100 -System.out.println ("Random:" + ((int) (Math.random () * 100) + 1)); -System.out.println ("--------------"); - A //public static int round (float a) rounded (self-study with double argument) +System.out.println ("Round:" + math.round (12.34f)); theSystem.out.println ("Round:" + math.round (12.56f)); -System.out.println ("--------------"); $ the //Public static Double sqrt (double A): positive square root theSystem.out.println ("sqrt:" +math.sqrt (4)); the } the}
The results are as follows:
Java Basic Knowledge Hardening 80:math class overview and Member Methods