scala 否则怎么写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12570005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
scala how to write if else
提问by Shahzeb Khan
I am an new learner of scala, and i am trying to run this sample code
我是 Scala 的新学习者,我正在尝试运行此示例代码
def isLast(c: Int, r: Int):Int ={
if(r == 1)
{
return 1;
}
else if (r == c){
return 1
}
}
But it gives me compile time error, saying
但它给了我编译时错误,说
Multiple markers at this line
- type mismatch; found : Unit required: Int
- type mismatch; found : Unit required: Int
Kindly help me, and also suggest me some good site for learning scala.
请帮助我,并建议我一些学习 Scala 的好网站。
回答by dhg
First, let's clean up your code a little:
首先,让我们稍微清理一下您的代码:
def isLast(c: Int, r: Int):Int = {
if(r == 1)
return 1
else if (r == c)
return 1
// but what about when r is neither 1 nor c ??
}
So you are telling scala that if ris 1, then return 1, and if r == c, return 1. That's fine. But if you want the method to return an Int, it has to return one in everycase. So Scala complains because it doesn't know what Int to return when ris neither 1 nor c.
所以你告诉scala如果r是1,则返回1,如果是,则r == c返回1。那很好。但是,如果您希望该方法返回一个 Int,则它必须在每种情况下都返回一个。所以 Scala 会抱怨,因为它不知道当 Intr既不是 1 也不是c.
The fix is to add an elseclause than returns some other Int.
解决方法是添加一个else子句而不是返回其他一些 Int。
As an additional note, you can and should leave out the returnkeyword here, letting Scala implicitly know that the result of the if-else expression, as the last expression in the function's body, should be returned:
作为附加说明,您可以并且应该在return此处省略关键字,让 Scala 隐式知道 if-else 表达式的结果,作为函数体中的最后一个表达式,应该返回:
def isLast(c: Int, r: Int):Int = {
if(r == 1)
1
else if (r == c)
1
else
0 // or some other Int
}
As a final note, if you have a function whose name starts with is, then it should probably return a Boolean. In other words, if the input is last, then return true, otherwise false.
最后要注意的是,如果您有一个名称以 开头的函数is,那么它可能应该返回一个布尔值。换句话说,如果输入是 last,则返回 true,否则返回 false。
回答by Luigi Plinge
I suspect you're trying to return 1as a substitute for true. Don't; it is not.
我怀疑你试图1作为true. 别; 它不是。
Your logic is:
你的逻辑是:
def isLast(c: Int, r: Int): Boolean = r == 1 || r == c
The : Booleantype annotation is optional. Here it's safe to leave it out because it's super-obvious what the return type is from the short expression on the right and your naming of the method. Don't use 10 lines of code when 1 will do.
该: Boolean类型的注释是可选的。在这里省略它是安全的,因为从右边的短表达式和你对方法的命名可以非常明显地看出返回类型是什么。不要使用 10 行代码,只要 1 行即可。
回答by asthasr
You have no "else" clause, just an "else if," so your function isn't guaranteed to return an integer. If it falls through both conditions, you'll return nothing, which is a "Unit" return type. You need to explicitly say else 0.
你没有“else”子句,只有一个“else if”,所以你的函数不能保证返回一个整数。如果它同时满足这两个条件,您将不返回任何内容,这是“单位”返回类型。你需要明确地说else 0。
回答by Viraj Wadate
Simply we can write If Else Statement:
我们可以简单地编写 If Else 语句:
scala> object TestIfElse{
def main(args: Array[String]){
var id = 10
if(id == 10)
println("Id is "+id)
else
println("Id is different")
} }
Call Main Method Using:
使用以下方法调用 Main 方法:
scala>TestIfElse.main(Array())
Output is:
输出是:
Id is 10

