Results 1 to 5 of 5
- 01-19-2012, 08:25 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
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?) } }
- 01-19-2012, 08:50 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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.
- 01-19-2012, 09:18 PM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: How to implement do..while loop with JOptionPane
...
while(n == 0);
- 01-19-2012, 09:28 PM #4
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.
- 01-20-2012, 06:29 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: How to implement do..while loop with JOptionPane
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
-
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
Need Help to Implement JWebmail
By pintushahk in forum Advanced JavaReplies: 0Last Post: 12-08-2010, 10:14 PM -
how to implement interface
By makpandian in forum New To JavaReplies: 1Last Post: 12-09-2008, 02:39 PM -
How to implement HttpSessionListener in JSP
By Srikanth816 in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 04-16-2008, 09:22 AM -
How can we implement IPC in java
By samson in forum NetworkingReplies: 1Last Post: 04-04-2007, 06:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks