Scala Int compareTo() 方法与示例
compareTo()方法用于在第一个整数值等于第二个整数值时返回 0,如果第一个整数值大于第二个整数值则返回 1,最后如果第一个整数值小于第二个整数值则返回 -1。
Method Definition: (First_Integer_Value).compareTo(Second_Integer_Value)
Return Type: It returns 0 if the first integer value is equal to the second integer value, 1 if the first one is greater then the second one and finally -1 if the first one is less than the second one.
示例 #1:
Scala
// Scala program of Int compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first Int number
val m1 = 3
// Applying compareTo method
val result = m1.compareTo(3)
// Displays output
println(result)
}
}
Scala
// Scala program of Int compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first Int number
val m1 = 3
// Applying compareTo method
val result = m1.compareTo(2)
// Displays output
println(result)
}
}
Scala
// Scala program of Int compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first Int number
val m1 = 3
// Applying compareTo method
val result = m1.compareTo(9)
// Displays output
println(result)
}
}
输出:
0
示例 #2:
斯卡拉
// Scala program of Int compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first Int number
val m1 = 3
// Applying compareTo method
val result = m1.compareTo(2)
// Displays output
println(result)
}
}
输出:
1
示例#3:
斯卡拉
// Scala program of Int compareTo()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating first Int number
val m1 = 3
// Applying compareTo method
val result = m1.compareTo(9)
// Displays output
println(result)
}
}
输出:
-1