Tag: 变量初始化

是否有可能在Kotlin的while条件体中初始化一个变量?

在下面的代码中: var verticesCount: Int // to read a vertices count for graph // Reading until we get a valid vertices count. while (!Assertions.checkEnoughVertices( verticesCount = consoleReader.readInt(null, Localization.getLocStr("type_int_vertices_count")))) // The case when we don't have enough vertices. println(String.format(Localization.getLocStr("no_enough_vertices_in_graph"), Assertions.CONFIG_MIN_VERTICES_COUNT)) val resultGraph = Graph(verticesCount) 我们在最后一行得到下一个错误: Error:(31, 33) Kotlin: Variable 'verticesCount' must be initialized Assertions.checkEnoughVertices接受一个安全的类型变量作为参数(verticesCount:Int),所以verticesCount不可能是未初始化的,或者在这里是空的(我们没有在这些行上得到相应的错误)。 当已初始化的变量再次被初始化时,最后一行发生了什么?