Search code examples
javaloopsdrop-down-menujoptionpane

Why wont it loop?


Thank you so much but now it not adding the correct price its keeping it at the first added amount for example it should be 6.99 +.20 +.20+.20 depending on how many I add

 public void addToppings() 
        {
            String[] topChoice = {"Pepperoni", "Mushrooms", "Pineapple", "Bacon", "Beef Bites(IDK)"};
            int add;
            String topPick = " ";
            String total = " ";
            double price = 0;
            int index = 0;

                do 
                    {
                       topPick = (String)JOptionPane.showInputDialog(null,"What toppings do you want?","Add Your Toppings", JOptionPane.QUESTION_MESSAGE, null, topChoice, topChoice[0]);

                       if ((topPick != null) && (topPick.length() > 0))
                                {
                                    price += 0.20;
                                    index++;
                                }

                    add = JOptionPane.showConfirmDialog(null,"Would you like to add more toppings?","More Toppings",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);           
                    }


                while (add == JOptionPane.YES_OPTION);

                    return;
   }

Solution

  • Move this line

    topPick = (String)JOptionPane.showInputDialog(null, "What toppings do you want?","Add Your Toppings", JOptionPane.QUESTION_MESSAGE, null, topChoice, topChoice[0]);
    

    into your do while loop.