为什么这段代码能够正确运行?有人告诉我多行循环体应该始终带有花括号
public class Sample {
public static void main(String[] args)
{
int[] nums = {1,2,3,4,5,6,7,8,9,10};
// print out whether each number is
// odd or even
for (int num = 0; num < 10; num++)
if (num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
}
}
最佳答案
使用多个语句(不是多行)时需要大括号。
然而,始终使用花括号是一个很好的做法。
这可以避免稍后添加语句时出现错误。
关于java - 多行循环体什么时候不需要花括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18196090/