我有以下代码:
import java.util.*;
public class Test {
public static void main(String[] args) {
boolean b = false;
if (b=true)
System.out.println("one. b = false");
if (b)
System.out.println("two. b = false");
}
}
输出为:
one. b = false
two. b = false
我将 b 设置为 false,那么为什么当 b 为 true 时它会打印语句?
最佳答案
你正在做分配,而不是比较
if (b=true)
你的意思是使用
if (b==true)
关于java - 如果 boolean 值为 true 但 boolean 值被声明为 false,为什么此代码会打印它应该打印的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30202899/