方法一代码更简洁,方法二思路更清晰。
三元运算符方法1:
public class Demo01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
System.out.println(max);
}
}
三元运算符方法2:
public class Demo02 {
//定义一个方法 返回3个int值的 最大值。
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int temp;
temp= (a>b)?a:b;
temp=(temp>c)?temp:c;
System.out.println(temp);
}
}