Common objects in Java--math

Source: Internet
Author: User

Math:

The class used for mathematical operations.

Member variables:

public static final Double PI

public static final Double E

Member Methods:

public static int abs (int a): absolute Value

public static double Ceil (double A): Rounding up

public static double floor (double A): Rounding down

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]

public static int round (float a) rounded ( self-study with double argument )

public static double sqrt (double A): positive square root

Demo

public class Mathdemo {

public static void Main (string[] args) {

public static final Double PI

System.out.println ("PI:" + Math.PI);

public static final Double E

System.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));

System.out.println ("--------------");

public static double Ceil (double A): Rounding up

System.out.println ("Ceil:" + Math.ceil (12.34));

System.out.println ("Ceil:" + Math.ceil (12.56));

System.out.println ("--------------");

public static double floor (double A): Rounding down

System.out.println ("Floor:" + Math.floor (12.34));

System.out.println ("Floor:" + Math.floor (12.56));

System.out.println ("--------------");

public static int max (int a,int b): Maximum Value

System.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));

System.out.println ("--------------");

public static Double pow (double a,double b): Power of b of a

System.out.println ("POW:" + Math.pow (2, 3));

System.out.println ("--------------");

public static Double Random (): random number [0.0,1.0]

System.out.println ("Random:" + math.random ());

get a random number between 1-100

System.out.println ("Random:" + ((int) (Math.random () * 100) + 1));

System.out.println ("--------------");

public static int round (float a) rounded ( self-study with double argument )

System.out.println ("Round:" + math.round (12.34f));

System.out.println ("Round:" + math.round (12.56f));

System.out.println ("--------------");

public static double sqrt (double A): positive square root

System.out.println ("sqrt:" +math.sqrt (4));

}

}

Please design a method that can achieve random numbers in any range.

Demo

Requirements: Please design a method that can achieve random numbers in any range.

Analysis:

A: keyboard input two data.

int strat;

int end;

B: find a way to get the random number between start and end

I write a function to achieve this effect and get a random number. (int)

C: Output This random number

 

public class Mathdemo {

public static void Main (string[] args) {

Scanner sc = new Scanner (system.in);

System.out.println (" Please enter start number:");

int start = Sc.nextint ();

System.out.println (" Please enter end number:");

int end = Sc.nextint ();

for (int x = 0; x < + × x + +) {

Invoke function

int num = Getrandom (start, end);

Output Results

SYSTEM.OUT.PRINTLN (num);

}

}

/*

* Write a function two explicit: return value type:int argument list:int start,int End

*/

public static int Getrandom (int start, int end) {

recall the random number between the 1-100 we talked about.

int number = (int) (Math.random () * 100) + 1;

int number = (int) (Math.random () * end) + start;

found a problem, how to do it ?

int number = (int) (Math.random () * (End-start + 1)) + start;

return number;

}

}

Common objects in Java--math

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.