Java help please

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:

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)
oh and here are my specs:

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.
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.

 
You guys suckage, I got help from my cousin who is a JAVA king son!! All of you (except chad & my cousin //content.invisioncic.com/y282845/emoticons/wink.gif.608e3ea05f1a9f98611af0861652f8fb.gif) are F@gs! :p

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);


		// while (userAnswer != correctNumber && counter <= 5 && counter >=1);

		if (counter <= 5 && counter >=1) {
		JOptionPane.showMessageDialog(null,"WOW !! That is correct and it only took you " + counter + " number of guesses ");
	System.exit(0); }

		// while (userAnswer != correctNumber &&  counter >= 6 && counter <= 10 );
		if (counter >= 6 && counter <= 10 ) {
		JOptionPane.showMessageDialog(null,"Well,That is correct but it took you " + counter +" number of guesses ");
	System.exit(0); }

		// while (userAnswer != correctNumber && counter > 10);
		if (counter > 10){
		JOptionPane.showMessageDialog(null,"Meh...That is correct, but it took you " + counter + " many guesses, thats ashame");
  System.exit(0); }


	System.exit(0);
}// ends main
}//ends class (program)
I was way off at end

 
OK i need more help now. its the last thing i need to do i think on this. I cant find in my notes anywhere how to replace a text box with astericks. //content.invisioncic.com/y282845/emoticons/frown.gif.a3531fa0534503350665a1e957861287.gif

like when a person inputs somehting it needs show up as astericks instead of their letters.

oh and i need to check to make sure they match each other when it checks....

Code:
 // create the password label
	labelPass = new JLabel ( );					//instantiate new jlabel
	labelPass.setText ("Password ");					//set label text to Phone #
	labelPass.setLocation(24,290);				//set location of jlabel
	labelPass.setSize(200,25);					//set size of jlabel
	labelPass.setForeground(Color.BLACK);//set initial background color
	contentPane.add(labelPass);					//add jlabel to content pane

	// create the password text box
	textPass = new JTextField ( );	//instantiate new JTextField
	textPass.setText("");						//clear jtextfield
	textPass.setToolTipText("Please type in password");
	textPass.setLocation(90,290);		//set location of jtextfield
	textPass.setSize(200,25);				//set size of jtextfield
	contentPane.add(textPass);			//add jextfield to content pane

		   // create the repassword label
	labelRepass = new JLabel ( );					//instantiate new jlabel
	labelRepass.setText ("Repassword ");					//set label text to Phone #
	labelRepass.setLocation(24,320);				//set location of jlabel
	labelRepass.setSize(200,25);					//set size of jlabel
	labelRepass.setForeground(Color.BLACK);//set initial background color
	contentPane.add(labelRepass);					//add jlabel to content pane

	// create the repassword text box
	textRepass = new JTextField ( );	//instantiate new JTextField
	textRepass.setText("");						//clear jtextfield
	textRepass.setToolTipText("Please type in password again");
	textRepass.setLocation(100,320);		//set location of jtextfield
	textRepass.setSize(200,25);				//set size of jtextfield
	contentPane.add(textRepass);			//add jextfield to content pane
then skipping ahead where the checks are made:

Code:
private boolean checkPass ()
{


	if (textPass.getText().length() == 0)
	{
		labelPass.setForeground(Color.RED);	//city is not correct
		return false;
	}
	else
	{
		labelPass.setForeground(Color.BLACK);	//city is correct
		return true;
	}
} //end of method

private boolean checkRepass () 
{


	if (textRepass.getText().length() == 0  )
	{
		labelRepass.setForeground(Color.RED);	//city is not correct
		return false;
	}
	else
	{
		labelRepass.setForeground(Color.BLACK);	//city is correct
		return true;
	}
} //end of method
here are my specs in particular this part

Code:
Add labels and textfields for phone, email, password, and retype password.  Make these 
textfields show **** as a user types in their password.  Adjust the GUI any way that you 
like as long as it looks good.
Code:
The checkPassword method needs to be added to program.  This method should 
check the text in the password textfield vs. the text in the retype password textfield.  If 
both of these passwords are equal, then both labels foregrounds (password and retype 
password ) should be set to original color of black.  If these passwords are not equal, 
then both labels foregrounds should be set to red
This the last thing i need help with on this nights HW //content.invisioncic.com/y282845/emoticons/frown.gif.a3531fa0534503350665a1e957861287.gif i am dying here

i know replacing needs to be like

labelPass.replace('insert here', 'here') // problem is that can only do single character replacements with that, unless i misunderstood

also "&" isnt working when setting that boolean condition to make sure they are both equal to each other

 
Activity
No one is currently typing a reply...

About this thread

Megalomaniac

5,000+ posts
Mr.SQ
Thread starter
Megalomaniac
Joined
Location
The Colony, TX
Start date
Participants
Who Replied
Replies
11
Views
330
Last reply date
Last reply from
Megalomaniac
IMG_20260516_193114554_HDR.jpg

sherbanater

    May 16, 2026
  • 0
  • 0
IMG_20260516_192955471_HDR.jpg

sherbanater

    May 16, 2026
  • 0
  • 0

New threads

Top