Scala:我如何使这些代码更简单?

时间:2016-11-17 18:09:25

标签: scala

如何使以下代码更简单?我对Scala很新。提前致谢!

示例1:

def xsum(n: Int): Int = {

  if (n<10) n else n%10+xsum(n/10)
}

示例2:

def num(n: Int): Int = {
  if (xsum(n)%10==0) n else (100-xsum(n))%10 + n*10
}

def xsum(n: Int): Int = {  
  if (n<10) n else n%10 + xsum(n/10)
}

示例3:

def tru(n: Int): Boolean = {
  n==0 || xsum(n)%10==0
}
def xsum(n: Int): Int = {
  if (n<10) n else n%10 + xsum(n/10)
}

1 个答案:

答案 0 :(得分:1)

def xsum(n:Int):Int= n.toString.map(_-'0').sum