首先,问题是“使用三元运算符编写Java程序查找三个数字中的最小值。”
以下是我的代码:
class questionNine
{
public static void main(String args[])
{
int x = 1, y = 2, z = 3;
int smallestNum;
smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : (z<y && z<x) ? z;
System.out.println(smallestNum + " is the smallest of the three numbers.");
}
}
我尝试在三元运算符中使用多个条件,但是不起作用。 我缺席了几天,所以我不太确定该怎么做,我的老师的电话没有开机。 有人能帮忙吗?
int smallest = min(min(x, y), z)
。 - JulietsmalledNum = min(x, min(y, z))
。 - Peter Lawrey