所以我正在摆弄java,并遇到了我试图编写的一些代码的问题。
我创建了一个类,LogicGate在该类中调用有方法将实例的状态设置LogicGate为 true 或 false。
LogicGate只有一个属性及其布尔值 true 或 false。
所有方法似乎都有效,但只有一个(称为 not()),我将布尔值更改为 false(如果 true)和 true(如果 false)。
该代码第一次将值从 true 更改为 false,但第二次它似乎忽略了 if 语句。
//Main
public class main {
public static void main(String[] args) {
LogicGate logicGate = new LogicGate(true);
System.out.println(logicGate.state());
System.out.println("");
logicGate.not(logicGate);
System.out.println("");
System.out.println(logicGate.state());
System.out.println("");
logicGate.not(logicGate);
}
}
//Class Logicgate
public class LogicGate {
//The attribute for the for the object
private boolean state;
//Creating the constructor for the object logic gate
public LogicGate(boolean logicGateState) {
state = logicGateState;
}
//This method changes the state of the Logic Gate
public void not(LogicGate obj) {
//Saving the current state of the logic gate
boolean currentState = obj.state();
System.out.println("....." + currentState);
//Checks the state of the object instance and changes it accordingly
if(currentState = true) {
obj.negate(obj);
System.out.println("The state has changed to: " + obj.state() + " Should be false");
}
else if(currentState = false) {
obj.set(obj);
System.out.println("The state has changed to: " + obj.state() + " Should be true");
}
}
//Sets the state of the object boolean variable to true
public void set(LogicGate obj) {
obj.setState(true);
}
//Sets the state of the object boolean variable to false
public void negate(LogicGate obj) {
obj.setState(false);
}
//This method will return the state of the object
//it can be either true or false
public boolean state() {
return state;
}
每次调用 not 方法时,布尔值都应该改变。
代码的最后一行应该说The state has changed to: true Should be true。
慕丝7291255
杨魅力
相关分类