Update AbsoluteValue.java

This commit is contained in:
Yang Libin 2019-08-15 09:12:33 +08:00 committed by GitHub
parent 9751ac5e98
commit b882515eb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,24 +4,21 @@ package Maths;
* @author PatOnTheBack
*/
public class AbsoluteValue {
public class AbsoluteValue {
public static void main(String[] args) {
int value = -34;
System.out.println("The absolute value of " + value + " is " + abs_val(value));
System.out.println("The absolute value of " + value + " is " + absVal(value));
}
public static int abs_val(int value) {
// If value is less than zero, make value positive.
if (value < 0) {
return -value;
/**
* If value is less than zero, make value positive.
*
* @param value a number
* @return the absolute value of a number
*/
public static int absVal(int value) {
return value > 0 ? value : -value;
}
return value;
}
}
}