首先,问题是“编写一个 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.");
}
}
我尝试在三元运算符中使用多个条件,但这不起作用。我缺席了几天,所以我真的不知道该怎么办,我老师的电话关机了。有什么帮助吗?
原文由 Mike 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试
您还可以删除括号: