[英]Java : if condition : using a variable inside the if as a condition of the if
As a beginner, I'm trying to code something like that.作为初学者,我正在尝试编写类似的代码。 For sure, the wrong way, because it doesn't work at all.
当然,错误的方式,因为它根本不起作用。 What I'm trying to do is: use the results i can get when I do something inside a if loop and use these results to calculate conditions the next time I enter the if... To be more understanable, I want to do the same thing if cond1 && cond2 are TRUE but not to early even if the situation occurs (delay of 1000) and not at a value under the previous one...
我想要做的是:使用我在 if 循环中执行某些操作时可以获得的结果,并在下次进入 if 时使用这些结果来计算条件......为了更容易理解,我想做如果 cond1 && cond2 为 TRUE 则相同,但即使情况发生(延迟 1000)也不早于前一个值...
I don't know how to code this, for the first time I enter the if and for the others... Can you help me please???我不知道如何编码,这是我第一次输入 if 和其他人......你能帮我吗? Thanks a lot.
非常感谢。
boolean cond_1 = a < 10;
boolean cond_2 = b < 15;
boolean cond_delay = (time_now - time_i_have_done_something) > 1000;
boolean cond_value = value_now > value_i_have_done_something;
if (cond_A1 == true && cond_A2 == true && cond_delay == true) {
do something;
long time_i_have_done_something = do_something.gettime();
double value_i_have_done_something = do_something.getvalue();
}
Example of the logic I want to implement:我要实现的逻辑示例:
if (cond_A1 == true) { // firt occurence at 7 h 30 min 00 sec 000 ms
time = gettime();
value = getvalue();
IOrder order = engine.submitOrder("EURUSD", instrument, orderCommand, 1);
}
if (cond_A1 == true) { // second occurence at 7 h 30 min 00 sec 190 ms
// do nothing because time between occurence#1 and occurence#2 < 1000 ms
}
if (cond_A1 == true) { // firt occurence at 7 h 30 min 01 sec 050 ms
// execute this because time between occurence#1 and occurence#3 > 1000 ms
time = gettime();
value = getvalue();
IOrder order = engine.submitOrder("EURUSD", instrument, orderCommand, 1);
}
This will loop, and before the action doSomething()
is called, if will check that a condition is true, and that 1 second has passed since the action was last taken.这将循环,并且在调用操作
doSomething()
之前,if 将检查条件是否为真,以及自上次执行操作以来已过去 1 秒。 It will also wait for the first action to be taken at a 1 second delay because the first call is not treated as a special case.它还将等待第一个操作延迟 1 秒,因为第一个调用不被视为特殊情况。 This can be fixed by declaring start like this:
这可以通过像这样声明 start 来解决:
long start = System.nanoTime() - timeBetween;
I put in a repeat variable with the value of 5. That can of course be removed.我放入了一个值为 5 的重复变量。当然可以将其删除。
I chose to not sleep()
the thread, but just yield()
for the processor to be able to take on other tasks while also being able to just busy-loop the program:我选择不
sleep()
线程,而只是yield()
处理器能够承担其他任务,同时也能够忙循环程序:
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
class StackOverflowTest {
public static void main(String [] args){
StackOverflowTest test = new StackOverflowTest();
test.notTooFast();
}
public void notTooFast(){
boolean condition1 = true;
long timeBetween = 1_000_000_000; // 1 second.
long start = System.nanoTime();
int repeat = 5; // run the loop only 5 times.
while (repeat > 0) { // or for an endless loop, use: while (true)
// has timeBetween passed since last time?
if (condition1 && (System.nanoTime() - start > timeBetween)) {
start = System.nanoTime(); // reset the time
doSomething(repeat--); // don't forget to decrease the counter.
} else {
Thread.currentThread().yield(); // hint to yield this threads current use of a processor.
}
}
}
// just print something for the sake of the example:
public void doSomething(int countdown){
System.out.println(LocalDateTime
.now()
.truncatedTo(ChronoUnit.MILLIS)
+ ": Doing something "
+ countdown);
}
}
It prints:它打印:
2020-07-15T13:30:34.409: Doing something 5
2020-07-15T13:30:35.350: Doing something 4
2020-07-15T13:30:36.350: Doing something 3
2020-07-15T13:30:37.349: Doing something 2
2020-07-15T13:30:38.350: Doing something 1
I imagine, the line我想,这条线
doSomething(repeat--);
should probably be replaced with something like this:可能应该用这样的东西代替:
repeat--;
IOrder order = engine.submitOrder("EURUSD", instrument, orderCommand, 1);
Instead of Thread.currentThread().yield();
而不是
Thread.currentThread().yield();
, you can also use , 你也可以使用
try {
Thread.currentThread().sleep(1000); // 1 second.
} catch (InterruptedException ex) {
// handle whichever way
Thread.currentThread().interrupt(); // reset interrupt flag
}
which will effective put the thread to sleep for 1 second.这将有效地使线程休眠 1 秒。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.