lundi 6 décembre 2021

How can I not order more than one bucket of popcorn (if statement)

Why can I type popcorn 2 or more times in a row

without giving me an error since I used set.add and second time when I type popcorn should give me an error since I can't have two same values in a set ? How can I do it so I can type only one time popcorn and give me an error second time when I try to type popcorn.

public void Popcorn() {
        boolean stop = false;
        while (!stop) {
            String s = scanner.nextLine();
            Set<String> set = new HashSet<>();
            if (s.equals("popcorn")&&!set.contains("popcorn")) {
                set.add("popcorn");
                int popcorn = 5;
                setPopCornValue(getPopCornValue() + popcorn);
                continue;
                }
     
            }
            if(s.equals("exit")){
                stop=true;
                break;
            }

    } ```

Aucun commentaire:

Enregistrer un commentaire