I wrote the following simple code:
def Commas(n: Long) = {
if (n >= 1000)
Commas(n/1000)
print(","+ n%1000/100 + n%100/10 + n%10)
else
print(n%1000/100 + n%100/10 + n%10)
}
While it seems correct to me, there is an error. What is wrong with the above code?
The If...else... syntax expects a statement. You can use a surrounding code block to ensure your code works as expected. Something like(also note that you have to specify return type to Unit or just remove the = sign):
def Commas(n: Long) {
if (n >= 1000) {
Commas(n/1000)
print(","+ n%1000/100 + n%100/10 + n%10)
}
else
print(n%1000/100 + n%100/10 + n%10)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With