我的代码有一个小问题,我用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()
}
}
您应该在System.out.print(" ")
之后断行或使用System.out.print(" "); i += 1
。否则,您只是在System.out.print(" ")
上调用Unit
类型的成员i
。