If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Need Help

Hello to all:
I need set up the java program for the "while statements" and the "for statements". I know how to do the "do while statements" as followsNote this "do while" is just a example of what I need to do with the "while" and "for" statements).

}while (userAnswer != correctNumber);
{// the curly brackets are needed because more than one statement for "if" are used.

But I also need to set this same program with the "while" only statements and then I need to set another program for the "for" only statements. In other words what would I change in the "do while" statements so I can use the "while statements" and what would I change so I can use the "for statements" Any help. Thankyou.

The while loop is a lot like the do while loop - just not a guaranteed first execution because the state of the loop condition might be false. You have a test of a boolean statement at the start, if it is true then perform the actions in the loop, and return to the test.

The for loop uses a "counter", a test of the counter, and self incrementing of the counter. As long as the test is true, then perform the actions in the loop. As you finish the loop the program increments the counter and tests again.

Certain actions or program structures are better for one or the other, but you can use both. Sometimes it makes sense only to use one or the other.

[by the way, you're going to need to name your class if you expect this to compile]

System.out.println("The random number generated was " + correctNumber);
do
{
String guess = JOptionPane.showInputDialog
("Pick a number between 1 and 100 - your guess? ");
userAnswer = Integer.parseInt(guess);
if (userAnswer < 1 || userAnswer > 100)//sets the limit to the numbers between 1 and 100.
{
JOptionPane.showMessageDialog
(null," You entered an incorrect number. You have taken " + tally
+ " guesses. Please try again.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (userAnswer > correctNumber)
{
JOptionPane.showMessageDialog
(null, "You guessed too high. Pick a number less than " + userAnswer
+ ". You have taken " + tally + " guesses.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (userAnswer < correctNumber)
{
JOptionPane.showMessageDialog
(null, "You guessed too low. Pick a number greater than " + userAnswer
+ ". You have taken " + tally + " guesses.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
}while (userAnswer != correctNumber);
{// the curly brackets are needed because more than one statement for "if" are used.
if (tally <= 5)
{
JOptionPane.showMessageDialog
(null, " WOW!! You guessed the correct number and your " + tally + " guesses was under 6 "
+ " tries, which is well above average. This game is done.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (tally <= 10)
{
JOptionPane.showMessageDialog
(null, " Good!! You guessed the correct number and your " + tally + " guesses was under 11 "
+ " tries, which is an average score. This game is done.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}
else if (tally >= 10)
{
JOptionPane.showMessageDialog
(null, " You guessed right, but I hope you pick my number sooner. It took you " + tally
+ " tries, which is way to many. This game is done.");
tally++;//calculates the number of attempts the users has made to find the correct number.
}//ends the last block JP statments.
} // ends the multi-statements for if.

Thankyou to all.

The information that everyone provided should be enough for me to manage to complete the dowhile and for statements. Now I have a general idea of the values that get inserted in the loops and what values or statements that can be used for the "ifs" or "else".