1.求三个数的最大值:
int max=a>b?(a>c?a:c):(b>c?b:c);
2.求绝对值:
方法一:
double result=Math.abs(input);
方法二:
double a1=(a>0?a:-a);
方法三:
package cn.cxy.exec;
import java.util.Scanner;
public class TestNum {
public static void main(String[] args) {
//1.接收用户输入的要测试的数字
System.out.println("请输入你要判断的数字:");
double input = new Scanner(System.in).nextDouble();
//2.调用求绝对值的方法并拿到方法执行后的结果(返回值)
double result = getAbsNum(input);
//3.打印求出的结果
System.out.println(input+"的绝对值是:"+result);
}
private static double getAbsNum(double input) {
if(input >=0){//如果是非负数
return input;//返回本身的值
}else{//如果是负数
return -input;//则值取反
}
}
}