/**
* 布尔运算和if-else条件语句的一些理解
* @author Administrator
*
*/
public class BooleanTest {
public static void main(String[] args){
boolean a = true;
boolean b = false;
boolean c = true;
if(a == true)
if(b == true)
if(c == true)
System.out.println("Some things are true in this world");
//else要与最近的if配对
else
System.out.println("Nothing is true in this world");
//else也要与最近的if配对,如果它的前面有else,就再往外一层配对if
else if(a&&(b=c))
System.out.println("It's too confusing to tell what is true");//这是为最后输出的语句
else
System.out.println("Hey this won't compile");
else
System.out.println("HaHa! I can come here");
System.out.println(b=c);//赋值运算,c的地址赋给b,b的值变为true,所以输出true
System.out.println(b!=c);//逻辑运算,b的值与c的值作不等于比较,true != true 所以输出为false
System.out.println(c=b);//赋值运算,b的地址赋给c,此时b已经为true,所以输出true
System.out.println(c!=b);//逻辑运算,true!=true 输出为false
System.out.println(b);//b为true,输出true
System.out.println(c);//c为true,输出true
//如果要写布尔值比较的话,最好写成true==c,可以避免少写等号的错误
if(true==c){
// ……
}
//如果写成
if(c=true){
// ……
}
//无论c是true还是false都会进入循环
}
}
* 布尔运算和if-else条件语句的一些理解
* @author Administrator
*
*/
public class BooleanTest {
public static void main(String[] args){
boolean a = true;
boolean b = false;
boolean c = true;
if(a == true)
if(b == true)
if(c == true)
System.out.println("Some things are true in this world");
//else要与最近的if配对
else
System.out.println("Nothing is true in this world");
//else也要与最近的if配对,如果它的前面有else,就再往外一层配对if
else if(a&&(b=c))
System.out.println("It's too confusing to tell what is true");//这是为最后输出的语句
else
System.out.println("Hey this won't compile");
else
System.out.println("HaHa! I can come here");
System.out.println(b=c);//赋值运算,c的地址赋给b,b的值变为true,所以输出true
System.out.println(b!=c);//逻辑运算,b的值与c的值作不等于比较,true != true 所以输出为false
System.out.println(c=b);//赋值运算,b的地址赋给c,此时b已经为true,所以输出true
System.out.println(c!=b);//逻辑运算,true!=true 输出为false
System.out.println(b);//b为true,输出true
System.out.println(c);//c为true,输出true
//如果要写布尔值比较的话,最好写成true==c,可以避免少写等号的错误
if(true==c){
// ……
}
//如果写成
if(c=true){
// ……
}
//无论c是true还是false都会进入循环
}
}