当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala Int compareTo()用法及代码示例


如果第一个整数值等于第二个整数值,则使用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


相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Scala Int compareTo() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。