aspose file tools*
The moose likes Beginning Java and the fly likes infinite loop problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "infinite loop problem" Watch "infinite loop problem" New topic
Author

infinite loop problem

vidu sri
Greenhorn

Joined: Jun 15, 2009
Posts: 8
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

Joined: Oct 01, 2001
Posts: 2771
    
  10

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

Joined: May 13, 2009
Posts: 679
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

Joined: Jan 28, 2008
Posts: 5575

Vidu:UseCodeTags
anandhi mohan
Greenhorn

Joined: Mar 24, 2009
Posts: 9
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

Joined: Jan 28, 2008
Posts: 5575

Anandhi : http://faq.javaranch.com/java/UseCodeTags
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: infinite loop problem
 
Similar Threads
primality formula
Java Rule Round-Up
Counting down with loops
Simple program: NEED HELP!!
My Array