"do while" loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robotlizz
    New Member
    • Sep 2008
    • 1

    "do while" loop

    Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the tutorials but when I change it from here I then get compiling issues... Can someone explain what I've done wrong?
    [code=java]
    import javax.swing.JOp tionPane;
    public class LloydPage_Assig nment5 {
    public static void main (String [] args) {

    // Enter Y to vote yes
    // Enter N to vote no
    // Enter Q to quit voting
    int quitVote = 0;
    int yesVote = 0;
    int noVote = 0;

    // charAt looks at number in parenthesis, returns char at that location
    // within string (the first character is at location 0)
    // firstChar is going to be 'Y' or 'N' or 'Q' etc.
    // Essentially it is a way to make a string into a char so that you may use string function

    String reply1 = JOptionPane.sho wInputDialog
    (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
    char userVote = reply1.charAt(0 );

    // If the user enters Y or y then add 1 to the number of yes votes

    do {
    yesVote = yesVote + 1;
    reply1 = JOptionPane.sho wInputDialog
    (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
    userVote = reply1.charAt(0 );
    } while ((userVote == 'Y') || (userVote == 'y'));

    // If the user enters N or n then add 1 to the number of no votes

    do {
    noVote = noVote + 1;
    reply1 = JOptionPane.sho wInputDialog
    (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
    userVote = reply1.charAt(0 );
    } while ((userVote == 'N') || (userVote == 'n'));

    // If the user enters any variable other than Y||y, N||n, Q||q then ignore vote

    do {
    reply1 = JOptionPane.sho wInputDialog
    (null, "Enter 'Y' to vote yes, 'N' to vote no, or 'Q' to quit voting");
    userVote = reply1.charAt(0 );
    } while ((userVote != 'Y') || (userVote != 'y') || (userVote != 'N') || (userVote != 'n') || (userVote != 'Q') || (userVote != 'q'));

    // If the user enters Q or q then
    // use a showConfirmDial og box
    // to ask if they really want to quit

    do {
    quitVote = JOptionPane.sho wConfirmDialog
    (null, "Are you sure you wish to Quit?");
    } while ((userVote == 'Q') || (userVote == 'q'));

    // Otherwise the loop ends and the totals must be shown in a
    // showMessageDial og box

    do {
    JOptionPane.sho wMessageDialog( null, "The total is number of Yes votes is " + yesVote + "\n The total is number of No votes is " + noVote);
    } while (quitVote == JOptionPane.YES _OPTION);

    }
    }[/code]
    Last edited by Nepomuk; Sep 24 '08, 03:19 PM. Reason: Added [CODE] tags
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Your logic is incorrect; you want to collect Y or N votes until the user wants to
    quit; then you want an additional confirmation from the user. If the user really
    wants to quit you display the totals and quit. Otherwise you just continue.
    Check your text book for the switch() statement and don't be afraid to add a
    few simple methods for sub-tasks. Here's a first idea:

    [code=java]
    for (char vote= 'A'; !quitVote(vote) ; ) {
    vote= getVote();
    switch (vote) {
    case 'y': case 'Y': yesVote++; break;
    case 'n': case 'N': noVote++; break;
    case 'q': case 'Q': quitVote++; break;
    default: illegalVote(vot e);
    }
    }
    [/code]

    The getVote() and illegalVote() methods handle what they have to handle.
    The quitVote() method checks whether or not the user really wants to quit.

    kind regards,

    Jos

    Comment

    Working...