if(true)
System.out.println("one");
System.out.println("two);
System.out.println("three);
起初它看起来很奇怪,但它确实有效。我的问题只是为了澄清:如果我不使用代码块,那么 if 之后的任何内容都会受到影响;如果我只在代码块内部使用代码块,就会受到影响——我说得对吗?还是通过这个例子我不知道发生了什么?
最佳答案
你的情况等同于:
if(true){
System.out.println("one");
}
System.out.println("two");
System.out.println("three");
输出将是
one
two
three
if
语句执行操作符(在你的例子中,它只是 System.out.println("one")
,紧随其后。图大括号( {}
) 也是运算符。例如:
if(false)
System.out.println("one");
System.out.println("two");
System.out.println("three");
/*output will be:
two
three
*/
//and in this case there will be no output
if(false){
System.out.println("one");
System.out.println("two");
System.out.println("three");
}
关于Java 语法 - 如果不使用代码块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955121/