简体   繁体   中英

Java | boolean doesn't re-evaluate / update

I'm trying to make a class that let's you repeat a task a certain amount of times, or until a condition is met. Before I go on, consider the following:

int test = 0;

And my repeat class:

public static abstract class Repeat {

    public void loop(int c) {
        for (int i = 0; i < c; i++) {
            run();
        }
    }

    public void until(boolean c) {
        while (!c) {
            run();
        }
    }

    public abstract void run();
}

The issue that I have is with the until(boolean c) method, not the loop(int c) method. The loop method works just fine, as proven below:

public static void main(String[] args) {
    new Repeat() {
        @Override
        public void run() {
            test += 1;
            System.out.println(test); // so I know what test currently is
        }
        {
            loop(10);
        }
    };
}

That changes test by 1, 10 times. The console proves that correct:

1
2
3
4
5
6
7
8
9
10

Now for the issue. If I try to use the until() method, it will get laggy if I don't terminate the launch within a few seconds, 'cause it will increment test infinitely.

public static void main(String[] args) {
    new Repeat() {
        @Override
        public void run() {
            test += 1;
            System.out.println(test); // so I know what test currently is
        }
        {
            loop(10);
        }
    };
    new Repeat() {
        @Override
        public void run() {
            test -= 1;
            System.out.println(test);
        }
        {
            until(test == 0);
        }
    };
}

That goes on forever. My assumption is that because the boolean that is passed in (test == 0) is never updated , so, if that is correct, can anyone tell me how to update it? And if my assumption is wrong, what causes this and how can I fix it?

Thanks to whoever actually reads every word I "typed" (mostly copy-pasted).

When you pass primitives in Java through methods, it passes the value, which means that you're passing only true or false , which never updates because it's just a value. If you make something like below, though, it should work:

public static abstract class Repeat {
    public void loop(int c) {
        for (int i = 0; i < c; i++) {
            run();
        }
    }

    public void until() {
        while (isLooping()) {
            run();
        }
    }

    public abstract void run();
    public abstract boolean isLooping();
}

Returning whether or not you wish to continue looping through isLooping() will allow you to update the value of that boolean of yours.

The value of test == 0 is false when you execute until(test == 0); . So you're passing false to until() , and inside until() , the value of !c is thus true and never changes.

You seem to think that passing a boolean expression to a method makes the method reevaluate the expression everytime c is evaluated. That's not how it works. The expression is evaluated before the call to the method is made, and the value of that expression is passed.

You start with test = 0 and your program only decrements test. so at the first if-clause its -1 them -2 .... its never 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM