Monday's Programming Question
This week's question is inspired by a piece of code that die.hard posted in the forum. The code comes from a book that he's using to learn Java. What surprised me is that it uses a combination of an infinite loop and the break statement. Personally, I use the break statement as a last resort as it breaks the logical flow of a program and makes it harder to read.
I've simplified the code so that it keeps the same style but is easier to see what's going on:
public class InfiniteLoop {
public static void main(String[] args) {
//boolean variable to determine if the loop should end
boolean shouldLoopEnd = false;
// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println ("The target number is: " + targetNumber);
//infinite loop begins...
while (true)
{
// generate a random number to try and match the target number
int guessNumber = (int) (Math.random() * 10);
System.out.println ("The loop guesses: " + guessNumber);
//check to see if the guess is correct
if (guessNumber == targetNumber)
{
shouldLoopEnd = true;
}
//if we've guessed correctly break loop
if (shouldLoopEnd == true) {
System.out.println ("Woo hoo! We have a match!");
break; // break loop
}
else
{
//around the loop we go again
System.out.println ("Let's try again..");
}
}
}
}
Can you rewrite the above code so that the program still works the same but it no longer uses the break statement? I'll post my version on Sunday.


public class InfiniteLoop {
public static void main(String[] args) {
//boolean variable to determine if the loop should end
boolean shouldLoopEnd = false;
boolean loopEnd=false;
// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println (”The target number is: ” + targetNumber);
//infinite loop begins…
while (loopEnd==false)
{
// generate a random number to try and match the target number
int guessNumber = (int) (Math.random() * 10);
System.out.println (”The loop guesses: ” + guessNumber);
//check to see if the guess is correct
if (guessNumber == targetNumber)
{
shouldLoopEnd = true;
}
//if we’ve guessed correctly break loop
if (shouldLoopEnd == true) {
System.out.println (”Woo hoo! We have a match!”);
loopEnd=true; // break loop
}
else
{
//around the loop we go again
System.out.println (”Let’s try again..”);
}
}
}
}
Can this help..Added another extra boolean variable…:)
Cheers
-S
This is mine a little bit reduce but i think is understandable….. i use both number to make the match on the while enjoy:
public class InfiniteLoop {
public static void main(String… args) {
// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println (”The target number is: ” + targetNumber);
int guessNumber=-1;
//pseudo-infinite loop begins…
while (guessNumber!=targetNumber)
{
//first Time we don’t want this
if(guessNumber!=-1)
System.out.println (”Let’s try again..”);
// generate a random number to try and match the target number
guessNumber = (int) (Math.random() * 10);
System.out.println (”The loop guesses: ” + guessNumber);
}
//is out mean that we did it
System.out.println (”Woo hoo! We have a match!”);
}
}
public class InfiniteLoop {
public static void main(String[] args) {
//boolean variable to determine if the loop should end
boolean shouldLoopEnd = false;
boolean loopEnd=false;
// generate a random number
int targetNumber = (int) (Math.random() * 10);
System.out.println (”The target number is: ” + targetNumber);
//infinite loop begins…
while (loopEnd==false)
{
// generate a random number to try and match the target number
int guessNumber = (int) (Math.random() * 10);
System.out.println (”The loop guesses: ” + guessNumber);
//check to see if the guess is correct
if (guessNumber == targetNumber)
{
shouldLoopEnd = true;
}
//if we’ve guessed correctly break loop
if (shouldLoopEnd == true) {
System.out.println (”Woo hoo! We have a match!”);
loopEnd=true; // break loop
}
else
{
//around the loop we go again
System.out.println (”Let’s try again..”);
}
}
}
}
Added another extra boolean variable