4 Replies - 950 Views - Last Post: 07 October 2012 - 04:44 AM Rate Topic: -----

#1 Atsuko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 12

How to stop the loop after 10 iterations?

Posted 06 October 2012 - 09:11 AM

Hi! I'm new here, and I'm starting to learn java (newbie).
So, I would like to know how to limit the loops.
This is my problem.

My problem is that I don't know how to exit this program after 10 iterations/loops.
(i know this is wrong ^^")

import javax.swing.JOptionPane;
public class Main {

    
    public static void main(String[] args) {
        
        String x;
        String y;
        int z=5;
        
        
        
        do      
            x=
            JOptionPane.showInputDialog("Enter any number other than "+z);
        while(!x.equals ("5"));

        if (x.equals ("5"))
            JOptionPane.showMessageDialog(null, "Hey! you weren't suppose to enter number "+z+"!");

        
            JOptionPane.showMessageDialog(null, "Wow, you're more patient than I "
            + "am, You win");

            
        
    }
        }


Is This A Good Question/Topic? 0
  • +

Replies To: How to stop the loop after 10 iterations?

#2 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: How to stop the loop after 10 iterations?

Posted 06 October 2012 - 09:22 AM

Here is an example of a for loop that will exit after executing 10 times, I use a for loop because I find more use with them, it easily be applied to a while, or do while loop.
public class example{

int counter; // variable used to end the for-loop

public static void main() {

for(counter = 0; counter < 10; counter++){
System.out.println(counter); //print the counter to show it executing 10 times.

     }
}


This code will execute 10 times, then stop. If you are wondering the syntax for a for-loop is
for(variable-initialization; continuation-condition; update;)

This post has been edited by Ytry: 06 October 2012 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 StefanOnRails  Icon User is offline

  • D.I.C Head

Reputation: 35
  • View blog
  • Posts: 106
  • Joined: 31-July 12

Re: How to stop the loop after 10 iterations?

Posted 06 October 2012 - 09:28 AM

You can use a counter and increment it each time a message is shown.
i = 0;
do{
   // code goes here
   i++;
}while(i < 10);


Was This Post Helpful? 0
  • +
  • -

#4 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: How to stop the loop after 10 iterations?

Posted 06 October 2012 - 10:08 AM

Assuming you also want to keep you initial requirement for continuing the loop:
int count = 0;
do 
{   
   x = JOptionPane.showInputDialog("Enter any number other than "+z);
}
while( (!x.equals("5")) && ++count < 10);

Was This Post Helpful? 0
  • +
  • -

#5 Atsuko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 12

Re: How to stop the loop after 10 iterations?

Posted 07 October 2012 - 04:44 AM

oh.. thank you thank you! >.< i got it now.. thanks to your help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1