Megalomaniac
5,000+ posts
Mr.SQ
OK My program works for the most part, but i cant seem to figure out how to do the end. I have 3 different conditions i need to do using a do/while statement. I dont think i am allowed to use cases. what ends up happening is it shows the 3 dialog boxes right after another instead of just one where the condition is met.
here is my code:
oh and here are my specs:
right now im doing the dowhile program
oh i am showing the random number in prompt to make it easier to test out //content.invisioncic.com/y282845/emoticons/wink.gif.608e3ea05f1a9f98611af0861652f8fb.gif
hopefully you guys can help me out.
here is my code:
Code:
// This is a sample Guessing Game done with do/whiles and if statements
// Mir Ali
// 4/1/08
// JDK used is 1.6
import java.awt.*;
import javax.swing.*;
public class DoWhile
{
public static void main (String args [] )
{
int correctNumber = (int) (Math.random()*100 +1); // comp picks number 1-100
int userAnswer =0; // userAnswer initialized to 0
System.out.println("The random number generated was " + correctNumber);
int counter = 1;
do
{
String guess = JOptionPane.showInputDialog("Pick a number between 1 and 100 - your guess?");
userAnswer = Integer.parseInt(guess);
if (userAnswer <1 || userAnswer >100)
{
JOptionPane.showMessageDialog(null,"You entered an incorrect number -- please try again; you guessed "+ counter + " number of times ");
counter++;
}
else if (userAnswer >correctNumber)
{
JOptionPane.showMessageDialog(null,"You guessed too high. Pick a number less than " + userAnswer + " you guessed "+ counter + " number of times ");
counter++;
}
else if (userAnswer <correctNumber)
{
JOptionPane.showMessageDialog(null,"You guessed too low. Pick a number higher than " + userAnswer + " you guessed "+ counter + " number of times ");
counter++;
}
}
while (userAnswer != correctNumber && counter <= 5 && counter >=1);
JOptionPane.showMessageDialog(null,"WOW !! That is correct and it only took you " + counter + " number of guesses ");
while (userAnswer != correctNumber && counter >= 6 && counter <= 10 );
JOptionPane.showMessageDialog(null,"Well,That is correct but it took you " + counter +" number of guesses ");
while (userAnswer != correctNumber && counter > 10);
JOptionPane.showMessageDialog(null,"Meh...That is correct, but it took you " + counter + " many guesses, thats ashame");
System.exit(0);
}// ends main
}//ends class (program)
Code:
• As the user guesses a number and gets it wrong, you should display the appropriate
error message about being wrong (already done) along with how many guesses they
have taken so far. These messages should be combined into one showMessageDialog
box.
• At the end, when they have guessed the correct number, the message should say
"Wow...." along with how many total guesses it took. It should also have a message
saying something special if they took 5 or less guesses total, something average if
they took 6 to 10 guesses, and something not so good if they took over 10 guesses.
• Make sure you use proper indentation. Also document (comment) the entire program.
• Submit 3 versions of this program. One with the “do/while loop” with above
adjustments, another with same program done with a “while loop”, and the last
version with a “for loop”. The programs should be given the following names:
ForLoop.java, WhileLoop.java, and DoWhile.java.
oh i am showing the random number in prompt to make it easier to test out //content.invisioncic.com/y282845/emoticons/wink.gif.608e3ea05f1a9f98611af0861652f8fb.gif
hopefully you guys can help me out.
