gpt4 book ai didi

scala - 错误 : ';' expected but 'else' found

转载 作者:行者123 更新时间:2023-12-04 00:43:31 30 4
gpt4 key购买 nike

我写了下面的简单代码:

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...else... 语法需要一个语句。您可以使用周围的代码块来确保您的代码按预期工作。类似的(另请注意,您必须将返回类型指定为 Unit 或仅删除 = 符号):

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)
}

关于scala - 错误 : ';' expected but 'else' found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15610648/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com