public double firstPastAsk(int rowIn) { ... System.out.println(trajectory(rowIn) + " " + primary(rowIn) + " " + finalAction(rowIn)); return pastPrice;}String action = " "; public String finalAction(int rowIn){ action = "[" + primary(rowIn) + "]"; return action ;}boolean buyFlag = false;public String primary(int rowIn){ String Request = "initial"; if(trajectory(rowIn) == "BUY") { if(buyFlag == false) { buyFlag = true; Request = "REQUEST BUY"; } } if(trajectory(rowIn) == "SELL") { buyFlag = false; Request = "REQUEST SELL"; } return Request;}输出结果: TRAJECTORY: PRIMARY: FINAL ACTION: WAITING ON SUFFICIENT DATA initial [initial] (BUY) REQUEST BUY [initial] initial [initial] (SELL) REQUEST SELL [REQUEST SELL] initial [initial] (BUY) REQUEST BUY [initial] (SELL) REQUEST SELL [REQUEST SELL] (BUY) REQUEST BUY [initial]使用上面的代码,当“finalAction”被调用,随后调用“primary”时,它接收“REQUEST SELL”和“initial”的返回值,但从不接收“REQUEST BUY”的值。我无法理解为什么“finalAction”总是收到“initial”而不是“REQUEST BUY”,但收到“REQUEST SELL”。
1 回答
一只斗牛犬
TA贡献1784条经验 获得超2个赞
每次firstPastAsk被调用,primary被调用两次。因为buyFlag是一个全局状态。当finalAction调用时primary,buyFlag状态已经被前一个改变了primary。
要解决此问题,您可以将第一次调用的结果传递primary给finalActionsoprimary只调用一次。
public double firstPastAsk(int rowIn)
{
...
String primaryResult = primary(rowIn);
System.out.println(trajectory(rowIn) + " " + primaryResult + " " + finalAction(primaryResult));
return pastPrice;
}
public String finalAction(String primaryResult)
{
action = "[" + primaryResult + "]";
return action ;
}
添加回答
举报
0/150
提交
取消