在Java中,我们通常可以在while
条件下执行赋值。但是Kotlin抱怨。所以下面的代码不会编译:while表达式中不允许赋值?
val br = BufferedReader(InputStreamReader(
conn.inputStream))
var output: String
println("Output from Server .... \n")
while ((output = br.readLine()) != null) { // <--- error here: Assignments are not expressions, and only expressions are allowed in this context
println(output)
}
根据这个其他thread,这似乎是最好的解决办法:
val reader = BufferedReader(reader)
var line: String? = null;
while ({ line = reader.readLine(); line }() != null) { // <--- The IDE asks me to replace this line for while(true), what the...?
System.out.println(line);
}
但是是什么呢?
真棒!用这个答案了解了很多!谢谢! – PedroD
啊,我提出了一个编辑,我对“它”的语法糖一无所知。令人惊叹,仍在学习:D – PedroD