如何求出三个数中的最大数?
首先我们来看版本一:
/**
* @author guqueyue
* @Date 2020/2/15
* 求三个数中的最大数
* 版本一
**/
public class ThreeMax1 {
public static void main(String[] args) {
// 随机产生三个固定序列的0-100之间的整数
Random random = new Random(47);
int a = random.nextInt(101);
int b = random.nextInt(101);
int c = random.nextInt(101);
System.out.println("生成的三个整数为:" + a + ", " + b + ", " + c);
// 假定a为最大值
if (a
return i
System.out.println(ternary(9));
System.out.println(ternary(10));
System.out.println(standardIfElse(9));
System.out.println(standardIfElse(10));
}
}
运行结果是一样的: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200215214427350.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1FpemhpX0h1,size_16,color_FFFFFF,t_70)
那么接下来我们便可以一行代码求出三个数中的最大值
/**
* @author guqueyue
* @Date 2020/2/15
* 求三个数中的最大数
* 版本二:运用三元操作符 if-else
**/
public class ThreeMax2 {
public static void main(String[] args) {
// 随机产生三个固定序列的0-100之间的整数
Random random = new Random(14);
int a = random.nextInt(101);
int b = random.nextInt(101);
int c = random.nextInt(101);
System.out.println("生成的三个整数为:" + a + ", " + b + ", " + c);
// 假定a为最大值, 用三目运算符求出最大值,赋值给a
a = (a > b && a > c) ? a : ((b > c) ? b : c);
System.out.println("最大值为:" + a);
}
}
大功告成,运行结果如下: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200215214812753.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1FpemhpX0h1,size_16,color_FFFFFF,t_70)
|