JAVA Tutorial
Numerous methods in the Java Math class let you work with numbers to solve mathematical problems.
The greatest value of x and y can be found using the Math.max(x,y) method:
Math.max(5, 10);
The lowest value of x and y can be found using the Math.min(x,y) method:
Math.min(5, 10);
The square root of x is returned by the Math.sqrt(x) function:
Math.sqrt(64);
The procedure Math.abs(x) yields the value of x in absolute (positive) terms:
Math.abs(-4.7);
A random number between 0.0 (inclusive) and 1.0 (exclusive) is returned by Math.random():
Math.random();
If you want more control over the random number, you can use the following formula, for instance, if you just want a random number between 0 and 100:
int randomNum = (int)(Math.random() * 101); // 0 to 100
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.