预期的嵌套if stataments问题;但是发现了

时间:2018-03-06 18:06:00

标签: scala

我正在我的大学参加CS课程的介绍,我们正在做一项修复一些代码的任务。我已经解决了大部分错误,但似乎无法理解为什么会发生这种错误。这是我的源代码:

import scala.io.StdIn._

println("Does you character have a hat?")
var answer = readLine()
val correctOne = (answer == "y")

if (correctOne) {
    println("Does you character have ears?")
    answer = readLine()
    val correctTwo = (answer == "y")

    if (correctTwo) {
        println("Your character is Jason.") 
    }
    else {
        println("Your character is Opus.")
    }
else {
    println("Does you character have three eyes?")
    answer = readLine()
    val correctThree = (answer == "y")
    }
    if (correctThree) {
        println("You character is Gina.")
    }
    else {
        println("You character is Albert.")
    }

}

这是我收到的错误:

C:\Users\Tyler\Documents\School\CSC10101\LAB\Lab6\fix.scala:24: error: ';' expected but 'else' found.
else {
^
one error found

我知道这个修复可能很容易,但我似乎无法看到它。任何帮助/解释都表示赞赏!

2 个答案:

答案 0 :(得分:2)

如果我理解你的意图正确,那么你们都错过了一个结尾括号}并包含了一个无关的}

import scala.io.StdIn._

println("Does you character have a hat?")
var answer = readLine()
val correctOne = (answer == "y")

if (correctOne) {
  println("Does you character have ears?")
  answer = readLine()
  val correctTwo = (answer == "y")

  if (correctTwo) {
    println("Your character is Jason.") 
  } else {
    println("Your character is Opus.")
  }
} else { // --> added } on this line
  println("Does you character have three eyes?")
  answer = readLine()
  val correctThree = (answer == "y")
  // } --> you don't need this }
  if (correctThree) {
    println("You character is Gina.")
  } else {
    println("You character is Albert.")
  }
}

答案 1 :(得分:2)

一点点缩进有助于:

public boolean currentMonthActivity(){ 
    selectSecondMonth.click(); 
    return Driver.findElements(By.xpath("//div[contains(@class,'ng-binding')]")[1]).size () > 0;
}

最近在新行的开头写import scala.io.StdIn._ println("Does you character have a hat?") var answer = readLine() val correctOne = (answer == "y") if (correctOne) { println("Does you character have ears?") answer = readLine() val correctTwo = (answer == "y") if (correctTwo) { println("Your character is Jason.") } else { println("Your character is Opus.") } } else { // missing closing `}` in this line println("Does you character have three eyes?") answer = readLine() val correctThree = (answer == "y") // one `}` too much here if (correctThree) { println("You character is Gina.") } else { println("You character is Albert.") } } 是一种新时尚吗?...

不要缩进四个空格,otherwise you will be paid less;)