class ee
{
public static void main(String args[])
{
boolean a;
a=true;
System.out.println("Answer is "+a);
a=false;
System.out.println("Answer is "+a);
if(a) System.out.println("This is not executed");
a=true;
if(a)
System.out.println("This is executed");
System.out.println("10>11 is "+(10>11));
}
}
输出:
Ansewr是真的 答案是错误的 这是执行 10> 11是假的
为什么在输出的第三行显示“这是执行的”?为什么输出中没有“这不执行”?
答案 0 :(得分:1)
让我们看一下代码的每一行。
class ee
{
public static void main(String args[])
{
boolean a;
a=true;
System.out.println("Answer is " + a); // OUTPUT Answer is true
a=false;
System.out.println("Answer is " + a); // OUTPUT Answer is false
//You are basically saying that if the statement is true, do this part(Inside the curly brackets)
// a = false here so the program will not do inside the curly brackets
if(a)
{
System.out.println("This is not executed");
}
a=true;
// a = true here so the program will do inside the curly brackets
if(a)
{
System.out.println("This is executed");
}
System.out.println("10>11 is "+(10>11));
}
}
我想您知道if语句,但如果您想了解有关使用if
语句的详情,请here
答案 1 :(得分:0)
因为a在第一个if中为false,而在第二个中为true。 if语句中的语句仅在if条件为真时执行
答案 2 :(得分:0)
a=false;
System.out.println("Answer is "+a);
if(a) System.out.println("This is not executed");
a=true;
if(a)
System.out.println("This is executed");
如果查看代码,则设置为false, 如果(a)默认假设a为真,则不显示 "这没有被执行"。 你再次设置为true,所以现在显示 "执行#34;。
答案 3 :(得分:0)
我希望它能帮到你,正确地完成每条评论,
boolean a; //default value false
a=true; //assinged 'a' to true
System.out.println("Answer is "+a); //so it prints true here
a=false; //again assigned it to false
System.out.println("Answer is "+a); // so it prints as false here
if(a) System.out.println("This is not executed");
/*
the above line means as if(a){// a is false here, then it wont go inside, HENCE IT IS NOT THERE IN OUTPUT
System.out.println("This is not executed");
}
*/
a=true; // again a assigned as true
if(a)
System.out.println("This is executed");
/*
the above line means as if(a){// a is true here, then it goes inside, HENCE IT IS THERE IN OUTPUT
System.out.println("This is executed");
}
*/
答案 4 :(得分:0)
a=false;
System.out.println("Answer is "+a);
if(a) System.out.println("This is not executed");
a=true;
if(a)
System.out.println("This is executed");
System.out.println("10>11 is "+(10>11));
a = false将a更改为false,因此if(a) System.out.println("This is not executed");
未执行,然后传递a=true;
使其成为真,这就是为什么执行System.out.println("This is executed");
bgot