如何解决Scala中的这种类型不匹配问题?
但是真的不知道如何修复(不要使用如何解决Scala中的这种类型不匹配问题?,scala,Scala,但是真的不知道如何修复(不要使用char.toList请)chars被声明为列表[char] 但是,您的第一个模式是”(“::Nil,这是一个列表[字符串],因为”(“是字符串-因此类型不匹配 您需要一个字符文字”(“”,而不是字符串文字”(“ 当然,其他模式也是如此。chars被声明为列表[Char] 但是,您的第一个模式是”(“::Nil,这是一个列表[字符串],因为”(“是字符串-因此类型不匹配 您需要一个字符文字”(“”,而不是字符串文字”(“ 当然,这同样适用于其他模式。Char文本使
char.toList
请)chars
被声明为列表[char]
但是,您的第一个模式是”(“::Nil
,这是一个列表[字符串]
,因为”(“
是字符串-因此类型不匹配
您需要一个字符文字”(“
”,而不是字符串文字”(“
当然,其他模式也是如此。
chars
被声明为列表[Char]
但是,您的第一个模式是”(“::Nil
,这是一个列表[字符串]
,因为”(“
是字符串-因此类型不匹配
您需要一个字符文字”(“
”,而不是字符串文字”(“
当然,这同样适用于其他模式。
Char
文本使用单引号,因此需要使用case'('::Nil=>false…
。Char
文本使用单引号,因此需要使用case'('::Nil=>false…
)。
def balance(chars: List[Char]): Boolean = {
if (chars.isEmpty == true) true
else transCount(chars, 0)
def transCount(chars: List[Char], pro: Int): Boolean = {
var dif = pro
chars match {
case "(" :: Nil => false
case ")" :: Nil => dif -= 1; if (dif == 0) true else false
case _ :: Nil => if (dif == 0) true else false
case "(" :: tail => dif += 1
transCount(tail, dif)
case ")" :: tail => dif -= 1;
if (dif < 0) false
else transCount(tail, dif)
case _ :: tail => transCount(tail, dif)
}
}
}
Error:(30, 13) type mismatch;
found : String("(")
required: Char
case "(" :: Nil => false
^