简体   繁体   中英

Scala: Type Mismatch found, Unit required: Boolean

I have been trying to run this code but somehow have hit the wall with 'unit mismatch, boolean expected error'. I have run through various questions on Stackoverflow but haven't found anything concrete that answers my question.

  def balance(chars: List[Char]): Boolean =
  {
    var i = 0;
    var j = 0;

    if (Count(i, j) == 0){
      true
    }
    else{
      false
    }

    def Count(count: Int, Pos: Int): Int = 
    {
            if (Pos == chars.length)
            {
                count
            }
            else
            {
                if (chars(Pos) == '(')
                {
                    Count(count + 1, Pos + 1);
                }
                else
                {
                    Count(count - 1, Pos + 1);
                }
            }
    }     
  } 

A code block delimited by {} evaluates to the last expression inside it. Here, your last expression is a definition ( def Count ), which evaluates to Unit . So move the expression you expect to be evaluated to the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM