比较三个值是否相等

编程入门 行业动态 更新时间:2024-10-09 13:25:40
本文介绍了比较三个值是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有人知道测试三个数字是否相同的捷径吗?我知道这行得通:

Does anybody know of a shortcut to test whether three numbers are the same? I know this works:

if number1 == number2 && number2 == number3 { }

但我想要一些更干净的东西,例如;

But I would like something cleaner, such as;

if number1 == number2 == number3 { }

这很重要,因为我要比较很多不同的值.

It's quite important as I'm comparing a lot of different values.

推荐答案

你可以利用元组的力量和平等的传递性.

You can use the power of tuples and the Transitive Property of Equality.

if (number1, number2) == (number2, number3) { }

只有当 number1 等于 number2 AND number2 等于number3.这意味着这3个值必须相等.

The clause of this IF is true only when number1 is equals to number2 AND number2 is equals to number3. It means that the 3 values must be equals.

更多推荐

比较三个值是否相等

本文发布于:2023-11-30 09:54:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649565.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!