简体   繁体   中英

If statements within while loop erroring

I'm trying to read about using while loops for pretest conditions for a small program that will compile responses and output data on them, but I'm having an issue where no matter what I enter in the input box it tells me it's invalid. I'm not sure what's wrong. Here's the relevant code.

import javax.swing.JOptionPane;

public class SurveySummarization
{
    public static void main(String[] args)
    {


        int agree = 0;
        int disagree = 0;
        int neutral = 0;
        int totalVotes = 0;
        int input;
        String inputString;


        inputString = JOptionPane.showInputDialog("Response: \n" + 
                    "(1=agree, 2=disagree, 3=no opinion, -1=exit)");
        input = Integer.parseInt(inputString);

        while (input != -1)
        {
            if (input == 1)
            {
                agree += 1;
                totalVotes += 1;
            }
            if (input == 2)
            {
                disagree += 1;
                totalVotes += 1;
            }
            if (input == 3)
            {
                neutral += 1;
                totalVotes += 1; 
            }
            else {
                JOptionPane.showMessageDialog(null, "invalid response "
                                        + input);
            }
        }


    }
}

It's because you're not using else 's properly. If you look at your code, your final if is

if (input == 3)
        {
            neutral += 1;
            totalVotes += 1; 
        }
        else {
            JOptionPane.showMessageDialog(null, "invalid response "
                                    + input);
        }

meaning if input != 3, show an invalid response.

To fix this, change the if's to else if (input == 2) ... (and the same for == 3).

As pointed out by Steve, the if 's are not properly put. I think you meant to put else if 's instead of just standalone if 's.

import javax.swing.JOptionPane;

public class SurveySummarization
{
    public static void main(String[] args)
    {


        int agree = 0;
        int disagree = 0;
        int neutral = 0;
        int totalVotes = 0;
        int input;
        String inputString;


        inputString = JOptionPane.showInputDialog("Response: \n" + 
                "(1=agree, 2=disagree, 3=no opinion, -1=exit)");
        input = Integer.parseInt(inputString);

        while (input != -1)
        {
            if (input == 1)
            {
                agree += 1;
                totalVotes += 1;
            }else if (input == 2)
            {
                disagree += 1;
                totalVotes += 1;
            } else if (input == 3)
            {
                neutral += 1;
                totalVotes += 1; 
            }
            else {
                JOptionPane.showMessageDialog(null, "invalid response "
                                    + input);
            }
        }

    }
}

Since you know input cannot be equal to 1 & 2 & 3 at the same time you should use else if's, along with the first if and final else. Your current code checks if input is equal to 1, if it is great. then you check if it's equal to 2, but your previous statement alright concluded that input was equal to 1, therefore you don't need to check for == 2, or == 3. Using if/else if/else chained together will only satisfy a single condition when chained together. once you hit a condition that satisfies your condition you skip the rest of the conditions.

if (input == 1)
{
    agree += 1;
    totalVotes += 1;
}
else if (input == 2)
{
    disagree += 1;
    totalVotes += 1;
}
else if (input == 3)
{
    neutral += 1;
    totalVotes += 1; 
}
else {
    JOptionPane.showMessageDialog(null, "invalid response " + input);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM