如果第一個整數值等於第二個整數值,則使用compareTo()方法返回0;如果第一個整數大於第二個整數,則返回1;如果第一個整數小於第二個整數,則返回-1。
函數定義: (First_Integer_Value).compareTo(Second_Interger_Vale)
返回類型: 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 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)
}
}
輸出:
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
相關用法
- Scala String compareTo()用法及代碼示例
- Scala Char compareTo()用法及代碼示例
- Scala Int CompareTo用法及代碼示例
- Scala Float compareTo()用法及代碼示例
- Scala Int self()用法及代碼示例
- Scala Int min()用法及代碼示例
- Scala map()用法及代碼示例
- Scala Map get()用法及代碼示例
- Scala Int to(end: Int)用法及代碼示例
- Scala Set ++()用法及代碼示例
- Scala Map min()用法及代碼示例
- Scala Int max()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Scala Int compareTo() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。