try this one... i hope this might help you a little bit..
this is a simple program that looks similar to what you want..
public class LoopSample9 {
public static void main(String[] args) {
int targetNumber,
guessNumber;
//boolean variable to determine if the loop should end
boolean loopEnd = false;
/**
* GENERATES THE TARGET NUMBER
* This one is generated by Math.floor( )
* Because the operation Math.random( ) * 10; has the range of
* 0.92839 - 9.83928398
*
* So we have to add the method Math.floor to return the
* LARGEST whole numbers that is LESS than or EQUAL to the
* generated random number. so the range will be still 0.923823 to 9.29839283
*
* Now we have to add + 1 to the operation Math.random ( ) * 10 + 1; to
* extend the range of the
* generated random number from 1.88738273 to 10.98293829
* So the range will now be 1.9887238723 - 10.92832983.
*
* Now if we add the method Math.floor( ),
* that is Math.floor(Math.random * 10) + 1;
* any random decimal number that will
* be generated by the Math.random( ) * 10 + 1;
* (e.g. 1.382983) or (e.g. 10.29839)
* will return the LARGEST whole number LESS than or EQUAL to
* that generated number(i.e. 1.0) or (i.e. 10.0)
*/
//to avoid the number "0" (zero)
targetNumber = (int) (Math.floor(Math.random() * 10) + 1);
System.out.println("Our Target Number Is: " + targetNumber);
System.out.println("\n");
//infinite loop begins here...
while (true) {
/**
* GENERATES THE GUESS NUMBER
* This is generated by Math.ceil( )
* This is slightly different from the operation and methods that
* generates the targetNumber
*
* Here, we now use the method Math.ceil, and as you can see we dont add + 1;
*
* Heres why..
* The operation Math.random( ) * 10; only generates a random number
* ranging from 0.29382938759 - 9.9829832983 (we already discuss it
* from the previews comments)
*
* So if we add the method Math.ceil( ) in this operation, that will be
* Math.ceil(Math.random( ) * 10;
* This operation will return a value of
* the Smallest number Greater than or equal to
* the generated random number (e.g. 0.982983) or (e.g. 9.823839)
* that will be (1.0 to 10.0)
*
* Its because the method Math.ceil( ) returns a value which is the Smallest Number
* GREATER than or EQUAL to that number .
*
* In this operation, if we add + 1 to the opetaion Math.ceil(Math.random * 10) + 1;
* it will generate a random number ranging from 1.2398983 to 10.2872873
* and by using the Math.ceil method it will return the value of
* the SMALLEST whole number
* GREATER than or EQAUL to that number (e.g 10.982398)
* it will return a number (i.e. 11.0)
*
* So we dont have to add + 1, or we dont have to
* extend the range of the Math.random( ) * 10;
*
* NOTE: Because by adding or using Math.ceil( ) method it
* will generate a random whole number
* ranging from 1 - 11;
*/
//to avoid the number "0" (zero)
guessNumber = (int) (Math.ceil(Math.random() * 10));
System.out.println("Your Guess Number Is: " + guessNumber);
/**
* check if the guess number is equal to the target number
* if guess number is equal to the target number
* the loop will end
*/
/**
* if the loopEnd ends..
* or the loopEnd is true
* then the loop will break or stop
*/
if (guessNumber == targetNumber) {
System.out.println("");
System.out.println("We Have A Match! ");
System.out.println("The Matching Number Is: " + guessNumber);
loopEnd = true;
break;
}
/**
* else if the loopEnd is false
* then it will continue to generate the loop statement
*/
else {
System.out.println("Let's Try Again!");
loopEnd = false;
}
}
}
}
or this one..
public class LoopSample_10 {
public static void main(String[] args) {
long targetNumber,
guessNumber;
targetNumber = 34;
System.out.println("Target Number Is: " + targetNumber);
do {
guessNumber = (int) (Math.ceil(Math.random() * 100));
System.out.println("Guess Number Is: " + guessNumber);
}
while (guessNumber != targetNumber);
if (guessNumber == targetNumber) {
System.out.println("\n");
System.out.println("Match Number Is: " + guessNumber);
}
}
}