Results 1 to 5 of 5
  1. #1
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default How to implement do..while loop with JOptionPane

    I am trying to write a code for a MadLib Game, and I am trying to ask the user if he wants to play again or not. I want to do a do..while loop that loops if the user chooses yes. I have not messed with do..while loops a lot, but I know there has to be a way to do this. My question is how do I identify in the code, to loop if the answer is yes. By the way, I have to use JOptionPane. Here is my code:

    Java Code:
    import javax.swing.JOptionPane;
    
    public class MadLib 
    {
    	public static void main(String[] args) 
    	{
    
    		do{
    		final int YES_NO_OPTION;
    		String name, adjective, noun, noun2, color, verb; 
    		name = JOptionPane.showInputDialog("Type a name..");
    		adjective = JOptionPane.showInputDialog("Type a descriptive adjective..");
    		noun = JOptionPane.showInputDialog("Type an animal name..");
    		verb = JOptionPane.showInputDialog("Type a verb..");
    		color = JOptionPane.showInputDialog("Type a color..");
    		noun2 = JOptionPane.showInputDialog("Type a noun..");
    
    		
    		JOptionPane.showMessageDialog(null, name + " had a " + adjective + " " + noun + "," + 
    			"\nit "+ verb + " like a " + color  + " " + noun2 + "!");
    		int n = JOptionPane.showConfirmDialog(null,
        		"Would you like to play again?",
        		"Try Again?",JOptionPane.YES_NO_OPTION);
    		}while();//what do I put here?)
    	
    	}
    }

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: How to implement do..while loop with JOptionPane

    Think about what the method JOptionPane.showMessageDialog() returns (which you've stored in the int variable n). That should tell you what the user's response is.

  3. #3
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: How to implement do..while loop with JOptionPane

    ...
    while(n == 0);

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: How to implement do..while loop with JOptionPane

    the variable n will not be recognized outside the do-block. so declare the variable n outside the do-block and while(n == 0) will work.

  5. #5
    gabrielpr12 is offline Member
    Join Date
    Nov 2011
    Posts
    41
    Rep Power
    0

    Default Re: How to implement do..while loop with JOptionPane

    Quote Originally Posted by j2me64 View Post
    the variable n will not be recognized outside the do-block. so declare the variable n outside the do-block and while(n == 0) will work.
    Thanks for the help. Here is the final code just in case someone has the same question:

    Java Code:
    import javax.swing.JOptionPane;
    
    public class MadLib 
    {
    	public static void main(String[] args) 
    	{
    		int n;
    		do{
    		final int YES_NO_OPTION;
    		String name, adjective, noun, noun2, color, verb; 
    		name = JOptionPane.showInputDialog("Type a name");
    		adjective = JOptionPane.showInputDialog("Type a descriptive adjective");
    		noun = JOptionPane.showInputDialog("Type an animal name");
    		verb = JOptionPane.showInputDialog("Type a verb");
    		color = JOptionPane.showInputDialog("Type a color");
    		noun2 = JOptionPane.showInputDialog("Type a noun");
    
    		
    		JOptionPane.showMessageDialog(null, name + " had a " + adjective + " " + noun + "," + 
    			"\nit "+ verb + " like a " + color  + " " + noun2 + "!");
    		n = JOptionPane.showConfirmDialog(null,
        		"Would you like to play again?",
        		"Try Again?",JOptionPane.YES_NO_OPTION);
    		}while(n==0);
    	
    	}
    }

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 03:12 AM
  2. Need Help to Implement JWebmail
    By pintushahk in forum Advanced Java
    Replies: 0
    Last Post: 12-08-2010, 11:14 PM
  3. how to implement interface
    By makpandian in forum New To Java
    Replies: 1
    Last Post: 12-09-2008, 03:39 PM
  4. How to implement HttpSessionListener in JSP
    By Srikanth816 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-16-2008, 10:22 AM
  5. How can we implement IPC in java
    By samson in forum Networking
    Replies: 1
    Last Post: 04-04-2007, 07:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •