• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

Repeat conditon until it retuns true

Hi! all

I want that if the condition below failed ..  the programme will try again to execute the arguments until the condition return true...

//  if ((clickedCount <= limit) & (fireButton.includes(x, y)))


private class MouseKeeper extends MouseAdapter{ //Modified class MouseKeeper
      public void mousePressed(MouseEvent e) {
 clickedCount++;

                  //locate position of mouse cursor when moused clicked
         int x = e.getX();
         int y = e.getY();
                  //if mouse clicked within firing region, create new
                  //ball and thread, and start thread
         if (fireButton.includes(x, y))
            balls.addElement(fireButton.fire(x, y));

     if ((clickedCount <= limit) & (fireButton.includes(x, y))){
          numberBalls = numberBalls - 1;
      ballsLabel.setText("Remaining Balls = " + numberBalls);
      }

      else {
         // Try again... code here..
      }

      }
   }
0
ZURINET
Asked:
ZURINET
  • 5
  • 3
2 Solutions
 
CEHJCommented:
Change

>>if ((clickedCount <= limit) & (fireButton.includes(x, y)))

to

while ((clickedCount <= limit) && (fireButton.includes(x, y))){
0
 
CEHJCommented:
Sorry - misread the question. You just need to execute it in a loop really


while (keepTrying) {
    // execute the code
}

but it's up to your application logic when you set keepTrying to false
0
 
ZURINETAuthor Commented:
What if the first condition return true..
then the app will keep on trying..?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
>>then the app will keep on trying..?

It'll keep trying until you tell it not to (you could do that once it's worked)
0
 
ZURINETAuthor Commented:
The system run out of memory.. ;-)
with the while statment
0
 
CEHJCommented:
Well it will if you don't tell it to stop trying at an appropriate moment ;-)
0
 
ZURINETAuthor Commented:
I want the code to minus 1 ball for every cilck.. and click should occour. when the condtion is met
((clickedCount <= limit) && (fireButton.includes(x, y)))
and then the programme should wait until the rest of the script is excuted..before repeating again...
0
 
objectsCommented:
> The system run out of memory.. ;-)
> with the while statment

Do *not* use a while loop there.


where is your thrtead getting started?
You can use the Thread method isAlive() to check if the thread is still running, or you can take appropriate action at the end of your threads run() method

0
 
CEHJCommented:
ZURINET, perhaps you could explain why you accepted that answer?
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now