【发布时间】:2017-11-23 12:05:36
【问题描述】:
我正在尝试比较 Scala 中的两个字符串。下面是函数。
def max(x:Int, y:String): String = {
| if (y=="Happy") "This is comparing strings as well"
| else "This is not so fair"
| if (x > 1) "This is greater than 1"
| else "This is not greater than 1"
| }
来自some answers 我假设我可以使用“==”符号来比较字符串。我给出了以下输入并得到了以下输出。我错过了什么或 Scala 的行为有所不同?
max (1,"Happy")
res7: String = This is not greater than 1
println(max(2, "sam"))
This is greater than 1
【问题讨论】:
-
我认为问题不在于
==,而在于您有两个 if/else.stackoverflow.com/a/12560532/3072566 -
谢谢@litelite。我会保留这个问题以供其他人参考。
-
@Dinesh,请接受答案。