简体   繁体   中英

Stuck in an infinite loop when trying to loop back to the beginning. Java

I am trying to get the code to loop if the user input an invalid size. It is stuck in an infinite loop though. I tried bringing "while" to the bottom and adding "do" to start the loop but I am either getting the same results or the code stops without looping back to the beginning.

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class BarkingLotJOP5
{
    public static void main(String[] args)
    {
        String size;
        Double sm = 8.00, md = 12.00, lg = 17.00, total, tax = .09;
        DecimalFormat df = new DecimalFormat("$###,###.##");

            JOptionPane.showMessageDialog(null,"Welcome to BarkingLot.com.");
            JOptionPane.showMessageDialog(null, "It is $5 for small, $10 for medium, and $15 for large.");

        size = JOptionPane.showInputDialog("Enter the dog's size.");

        while
            (!size.equals("small") || size.equals("medium")|| size.equals("large"))
                {
        if
            (size.equalsIgnoreCase("small"))
               {
                 total = (sm*tax)+sm;
                 JOptionPane.showMessageDialog(null,"For a small dog, your total is "+ df.format(total)+
                                                                      "\nThank you, have a great day!");
               }
        else
        if
            (size.equalsIgnoreCase("medium"))
               {
                 total = (md*tax)+md;
                 JOptionPane.showMessageDialog(null,"For a medium dog, your total is "+ df.format(total)+
                                                                               "\nThank you, have a great day!");
               }
        else
        if
            (size.equalsIgnoreCase("Large"))
                           {
                             total = (lg*tax)+lg;
                             JOptionPane.showMessageDialog(null,"For a large dog, your total is "+ df.format(total)+
                                                            "\nThank you, have a great day!");
                    }
        else
            JOptionPane.showMessageDialog(null,"Error");
               }
    }
}
while(!(size.equals("small") || size.equals("medium")|| size.equals("large")))

(Notice the braces). Basically your code checked like:

Keep looping if:

Size is not equal to: small

or

Size is equal to: medium

or

Size is equal to: large

But you want:

Keep looping if:

Size is not equal to (small or medium or large)

EDIT:

In the else condition instead of:

else
            JOptionPane.showMessageDialog(null,"Error");

you can use:

else{
    JOptionPane.showMessageDialog(null,"Error, try again");
    size = JOptionPane.showInputDialog("Enter the dog's size.");
}

This will make the input box appear again and set the value of size again to newer value.

break statement will do the trick for you.

And remove the ! sign in front of !size.equals("small")

    while (size.equals("small") || size.equals("medium") || size.equals("large")) {
        if (size.equalsIgnoreCase("small")) {
            total = (sm * tax) + sm;
            JOptionPane.showMessageDialog(null, "For a small dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        } else if (size.equalsIgnoreCase("medium")) {
            total = (md * tax) + md;
            JOptionPane.showMessageDialog(null, "For a medium dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        } else if (size.equalsIgnoreCase("Large")) {
            total = (lg * tax) + lg;
            JOptionPane.showMessageDialog(null, "For a large dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        } else {
            JOptionPane.showMessageDialog(null, "Error");
            break;
        }
    }

Update :

I recommend you to use switch instead of while in this case. It looks more accurate for me.

    switch (size) {

        case "small":
            total = (sm * tax) + sm;
            JOptionPane.showMessageDialog(null, "For a small dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        case "medium":
            total = (md * tax) + md;
            JOptionPane.showMessageDialog(null, "For a medium dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        case "large":
            total = (lg * tax) + lg;
            JOptionPane.showMessageDialog(null, "For a large dog, your total is " + df.format(total)
                    + "\nThank you, have a great day!");
            break;
        default:
            JOptionPane.showMessageDialog(null, "Error");
            break;

    }

Prompt in the loop.

do { size = JOptionPane.showInputDialog("Enter the dog's size.");

blah blah blah

}
while (!(size.equals("small") || size.equals("medium") || size.equals("large")))

Add parentheses to while.

Rest is fine

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