while(true) loops, while functional, are somewhat of a poor code design as they are sometimes difficult to decipher what a section of code is suppose to do. That's not to say never use while(true) loops, as sometimes they are much easier to read than the alternatives (or produce shorter code), but try to use some of these alternatives before resorting to while(true) loops.
Ex. alternative: Use a boolean flag to determine if you can quit.
boolean canQuit = false;
while(!canQuit)
{
// <TODO> do test one
if(test1_OK)
{
// <TODO> do test two
if(test2_OK)
{
// <TODO> do test three
if(test3_OK)
{
// can quit
canQuit = true;
}
}
}
}
Here you can easily tell what the code is suppose to do and quickly follow each possible logic path.
Something else you can do to make your code easier to read (and write/manage) is to create "helper methods". These methods abstract away the details when you want to get the general picture of what some code is doing, and keep your code from getting bloated when you want the same code to be executed multiple times. Something like this:
boolean canQuit = false;
while(!canQuit)
{
if(doTest1() == true) // doTest1 is a method that you write to perform some stuff and returns true on success and false on fail
{
if(doTest2() == true) // doTest2 is a method that you write to perform some stuff... etc. etc.
{
if(doTest3() == true) // doTest3 is a method that you write to perform some stuff... etc. etc.
{
canQuit = true;
}
}
}
}
then you just need to implement the doTest# methods somewhere and you have easily maintainable code
public static boolean doTest1()
{
System.out.println("did test1 succeed (y/n)?");
if(new Scanner(System.in).next().equals("y"))
{
// test succeeded
return true;
}
else
{
// test failed
return false;
}
}