Java 三元算子的多重条件
首先,问题是“编写一个Java程序,使用三元运算符查找三个数字中的最小值。” 这是我的密码:Java 三元算子的多重条件,java,operator-keyword,ternary,Java,Operator Keyword,Ternary,首先,问题是“编写一个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 &&
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.");
}
}
第九类问题
{
公共静态void main(字符串参数[])
{
int x=1,y=2,z=3;
int smallestNum;
smallestNum=(x最后一部分:(zTry
intmin=x
您还可以删除括号:
int min = x < y ? x < z ? x : z : y < z ? y : z;
intmin=x
因为这是家庭作业,所以我不只是给你答案,而是给你一个算法,让你自己来计算
首先了解如何使用单个三元运算符写入min(x,y)
一旦你有了它,将下面的min(x,y,z)代码改为使用三元运算符,然后在代码中替换上一步计算出的min(x,y)
int min(x, y, z) {
if (x <= y) {
return min(x, z);
} else {
return min(y, z);
}
}
int最小值(x,y,z){
如果(x你正在测试z,而你真的不需要。你的三元运算符的形式必须是cond?ifTrue:ifFalse
所以如果你有多个条件,你有:
cond1?如果为真1:cond2?如果为真2:ifFalse2
如果你明白这一点,不要往下看。如果你仍然需要帮助,请往下看
我还包括了一个不嵌套它们的版本,这个版本更清晰(假设你不需要嵌套它们。我当然希望你的作业不需要你嵌套它们,因为这太难看了!)
以下是我的想法:
class QuestionNine
{
public static void main(String args[])
{
smallest(1,2,3);
smallest(4,3,2);
smallest(1,1,1);
smallest(5,4,5);
smallest(0,0,1);
}
public static void smallest(int x, int y, int z) {
// bugfix, thanks Mark!
//int smallestNum = (x<y && x<z) ? x : (y<x && y<z) ? y : z;
int smallestNum = (x<=y && x<=z) ? x : (y<=x && y<=z) ? y : z;
System.out.println(smallestNum + " is the smallest of the three numbers.");
}
public static void smallest2(int x, int y, int z) {
int smallest = x < y ? x : y; // if they are equal, it doesn't matter which
smallest = z < smallest ? z : smallest;
System.out.println(smallest + " is the smallest of the three numbers.");
}
}
第九类问题
{
公共静态void main(字符串参数[])
{
最小(1,2,3);
最小(4,3,2);
最小(1,1,1);
最小(5,4,5);
最小(0,0,1);
}
公共静态无效最小值(整数x,整数y,整数z){
//修正错误,谢谢马克!
//intsmallestnum=(xintmin=(xpublicstaticintmin(intx,inty,intz){
返回x我的解决方案:
public static void main(String args[])
{
int x = 1, y = 2, z = 3;
int smallestNum = (x < y && x < z) ? x :
(y < x && y < z) ? y :
(z < y && z < x) ? z:y;
System.out.println(smallestNum + " is the smallest of the three numbers.");
}
publicstaticvoidmain(字符串参数[])
{
int x=1,y=2,z=3;
int smallestNum=(x
我的贡献
public static int getSmallestNumber( int x, int y, int z) {
return x < y && x < z ? x : y < x && y < z ? y : z;
}
public static void main ( String ... args ) {
System.out.println( getSmallestNumber( 123, 234, 345 ) );
}
public static int getSmallestNumber(int x,int y,int z){
返回x
最好的方法是使用if和else语句制作一个示例,然后对其应用三元运算符
(q我知道已经很晚了。仅供参考:
int smallestNum = (x<y ? x : (x=y)) < z ? x : z
intsmallestnum=(x这个答案迟了七年才出来,所以我只给你代码:
int smallestNumber = (x > y) ?
((y > z) ? z : y) :
((x > z) ? z : x);
缩进应该解释代码,它只是一个在初始条件下计算其他三元数的三元数。
x>y
;/*如果该条件为真,则计算第一个三元数,否则计算第二个三元数。*/您错过了z var之后的值
smallestNum=(xtry it,而不是上一个测试,至少在我的经验中,只放“:z”并不意味着你应该写链式三元语句。此外,到目前为止,你所拥有的非常复杂,你只需要将变量相互比较一次:int minimest=min(min(x,y),z)
.Math.min使用三元运算符。最简单的方法是静态导入此方法并写入smalledNum=min(x,min(y,z))
考虑在教授做出“三”时修改可能事件的赋值一个变量。这可能会给你额外的分数。无括号的版本看起来像是模糊C竞争的一个条目。为什么添加额外的偏执会产生错误:意外类型int min=x
请在回答中添加更多详细信息我喜欢这个实现。让库来做这项工作。虽然它改变了数据,但我可以看到,当使用参数(3,2,1)@fthdgn~调用此工作时,返回2。很好,已修复。
public static void main(String args[])
{
int x = 1, y = 2, z = 3;
int smallestNum = (x < y && x < z) ? x :
(y < x && y < z) ? y :
(z < y && z < x) ? z:y;
System.out.println(smallestNum + " is the smallest of the three numbers.");
}
public static int getSmallestNumber( int x, int y, int z) {
return x < y && x < z ? x : y < x && y < z ? y : z;
}
public static void main ( String ... args ) {
System.out.println( getSmallestNumber( 123, 234, 345 ) );
}
int smallestNum = (x<y ? x : (x=y)) < z ? x : z
int smallestNumber = (x > y) ?
((y > z) ? z : y) :
((x > z) ? z : x);