Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

infinite loop problem

 
vidu sri
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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");
}
}
}
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • 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;
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidu:UseCodeTags
 
anandhi mohan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anandhi : http://faq.javaranch.com/java/UseCodeTags
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic