Scala Value不是Unit的成员

时间:2014-06-04 09:27:01

标签: scala

我对我的代码有一个小问题,我在java及其工作中编写代码,但由于某种原因它在Scala中不起作用。任何想法?

def main(args: Array[String]) {

    val in = new Scanner(System.in)
    var T: String = null
    var P: String = null
    var cand: String = null
    var pos: Int = 0
    var i: Int = 0

    System.out.print("Enter a text string T: ")
    T = in.nextLine()
    System.out.print("Enter a pattern string P: ")
    P = in.next()
    println()
    pos = 0

    while (pos <= T.length - P.length) {
      cand = T.substring(pos, pos + P.length)
      if (P == cand) {
        println(T)
        i = 0
        while (i < pos) {
          System.out.print(" ")i += 1 // Error : Value i is not a member of Unit
        }
        println(P)
        println()
      }
      pos += 1
    }
    in.close()
  }
}

1 个答案:

答案 0 :(得分:5)

您应该在System.out.print(" ")之后中断或使用System.out.print(" "); i += 1。否则,您只需在i System.out.print(" ")上呼叫成员Unit