SOLVED. thanks snowguy13!!
Hello everyone. I'm currently working on an assignment that requires me to have a loop counting votes until q is entered to activate a dialog box asking if I really want to quit. If I hit yes, the program ends and votes are tallied. If I hit any other key, it goes back to the first loop where it counts votes. I'm having trouble with this because when I set up the showConfirmDialog box, I have to use a while loop. When I do this, I have already left the scope of the first while loop and do not know how to re-enter it.
Can someone help me out with this? I just need advice, I don't want the code written for me or anything. Thank you.
public class VoteCount { public static void main(String[] args) { int yesVotes = 0, noVotes = 0, totalVotes = 0; char vote; { do { String voteString = JOptionPane.showInputDialog(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting"); vote = voteString.charAt(0); if (vote == 'Y' || vote == 'y') yesVotes++; if (vote == 'N' || vote == 'n') noVotes++; } while (vote != 'Q' && vote != 'q'); } totalVotes = yesVotes + noVotes; if (vote == 'Q' || vote == 'q') { int reply = 1; while (reply != JOptionPane.YES_OPTION) reply = JOptionPane.showConfirmDialog(null, "Quit?", "Warning!", JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) JOptionPane.showMessageDialog(null, "yes " + yesVotes + " no " + noVotes + " total " + totalVotes); } } }
package javaapplication16; import javax.swing.JOptionPane; public class VoteCount { public static void main(String[] args) { int yesVotes = 0, noVotes = 0, totalVotes = 0; totalVotes = yesVotes + noVotes; char vote; int reply = 1; { do { String voteString; voteString = JOptionPane.showInputDialog(null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting", "Vote Now!", JOptionPane.OK_CANCEL_OPTION); vote = voteString.charAt(0); if (voteString == null || voteString.length() == 0) vote = 'q'; if (voteString.toLowerCase().equals("y")) yesVotes++; if (voteString.toLowerCase().equals("n")) noVotes++; { } if (voteString.toLowerCase().equals("q")) { reply = JOptionPane.showConfirmDialog(null, "Quit?", "Warning!", JOptionPane.YES_NO_OPTION); if (reply == JOptionPane.YES_OPTION) JOptionPane.showMessageDialog(null, "yes " + yesVotes + " no " + noVotes + " total " + totalVotes); else if (reply != JOptionPane.YES_OPTION); continue; } } while (reply != JOptionPane.YES_OPTION); } } }