當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Scala Int to(end: Int)用法及代碼示例

to(end:Int)方法用於返回從指定的int到“ end”的範圍,該範圍在參數列表中給出,但此處“step”未在參數列表中指定。

函數定義: def to(end:Int):Inclusive

返回類型: It returns the range.



範例1:

// Scala program of Int to()  
// method  
    
// Creating object  
object GfG  
{   
    
    // Main method  
    def main(args:Array[String])  
    {  
        // Applying to() method   
        val result = 5.to(10)  
            
        // Displays output  
        println(result)  
        
    }  
}  
輸出:
Range(5, 6, 7, 8, 9, 10)

範例2:

// Scala program of Int to()  
// method  
    
// Creating object  
object GfG  
{   
    
    // Main method  
    def main(args:Array[String])  
    {  
        // Applying to() method   
        val result = 20.to(25)  
            
        // Displays output  
        println(result)  
        
    }  
}  
輸出:
Range(20, 21, 22, 23, 24, 25)


相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Scala Int to(end: Int) method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。