• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

infinite loop problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if its successful,i want to end the program.if its not,to generate a random number and check with it.... i dont get the desired output.


class dotcomm
{
public static void main(String[] args)
{
dd d =new dd();
d.game();
}
}
class dd
{
int hit=0;
int[] locations = {2,3,4};
void game()
{
while(true)
{
int userg=(int)(Math.random()*4);
System.out.println("the guess is"+userg);
for(int num:locations)
{
if(userg==num)
{
System.out.println("successful");
//hit++;
//System.out.println("the num of hits ="+ hit);
break;
}
}
System.out.println("does not match,TRY AGAIN");
}
}
}
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, you should use Code tags when you are posting code here.

The "break" only breaks out of the for loop, not the while loop, which is the infinite one. You need a labeled break, or just a return instead.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Greg Charles wrote:First, you should use Code tags when you are posting code here.

The "break" only breaks out of the for loop, not the while loop, which is the infinite one. You need a labeled break, or just a return instead.



In addition to what Greg suggests, another possibility to exit the infinite loop is to use a Boolean variable in the while condition e.g.

Boolean loop = true;

while(loop) {

then when you hit this condition that causes the break, you can just leave the break; as is and add a line of code just before the break that sets loop to false;
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vidu:UseCodeTags
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your code

while(true)
{

for(....)
{
if(condition)
{

break

}
}

}

you are using break inside for loop so It will come out from forloop only not from while loop . If you want to break the while loop
then you can set flag to break it.


try this:



class dotcomm
{

public static void main(String[] args)
{
dd d = new dd();
d.game();
}
}

class dd
{

int hit = 0;
int[] locations =
{
2, 3, 4
};

void game()
{
boolean flag = true;
while (flag)
{
int userg = (int) (Math.random() * 4);
System.out.println("the guess is" + userg);
for (int num : locations)
{
if (userg == num)
{
System.out.println("successful");
//hit++;
//System.out.println("the num of hits ="+ hit);
flag = false;
break;

}
}
if (flag)
{
System.out.println("does not match,TRY AGAIN");
}
}
}
}
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anandhi : http://faq.javaranch.com/java/UseCodeTags
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic