質問

I want to repeat calling a function in WHILE(), i.e.

WHILE (temp=pop())
    check temp
    if found return true
    else loop
IF finished looping but not found
    return false

But I can't seem to implement the WHILE (temp=pop()). Is there any other way to do this?

Below is my attempt on coding it:

while(c1=g1.pop()){
   if (c1.regis.equals(r) == false) {
       np1.enqueue(c1.regis, 'a');
       counter++;
   }else if (c1.regis.equals(r) == true) {
       while (np1.isEmpty() != true) {
            c2 = np1.dequeue();
            g1.push(c2.regis, c2.status);
       }
       counter = g1.checkSpace();
       return true;
   }else{
       return false;
   }
}

while(c1=g1.pop()) can't work, and i can't return true or false in a while loop

役に立ちましたか?

解決 2

I added a variable

boolean status = false;

to store the status of the function.

public boolean departure(String r, char t) {
        int counter = 0;
        boolean status = false;
        do {
            c1 = g1.pop();
            if (c1.regis.equals(r) == false) {
                np1.enqueue(c1.regis, 'a');
                counter++;
            } else if (c1.regis.equals(r) == true) {
                while (np1.isEmpty() != true) {
                    c2 = np1.dequeue();
                    g1.push(c2.regis, c2.status);
                }
                status = true;
                counter = g1.checkSpace();
            } else {
                status = false;
            }
        } while (counter < g1.checkSpace());
        return status;
    }

this allowed me to return the actual status of the function and repeat popping at the same time.

他のヒント

The expression provided to while must be a boolean in Java, but c1=g1.pop() evaluates to the new value of c1.

You need something like this:

while (!stack.isEmpty()) {
    MyObject next = stack.pop();
    //...
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top