Scala Value 不是 Unit 的成员
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24033728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala Value is not a member of Unit
提问by user3706354
iam having small problem with my code, i make the code at java and its works but for some reason it wont work in Scala. any idea ?
我的代码有小问题,我在 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()
}
}
回答by Peter
You should break the line after System.out.print(" ")or use System.out.print(" "); i += 1. Otherwise, you are simply calling the member ion System.out.print(" ")which is of type Unit.
您应该在System.out.print(" ")或之后换行System.out.print(" "); i += 1。否则,你只是调用成员i在System.out.print(" ")这类型的Unit。

