添加单词并检查Arraylist Hangman

时间:2015-11-23 14:07:41

标签: java loops arraylist

我在代码中添加单词和使用do时遇到了问题:

ArrayList<String> word = new ArrayList<>();
word.add("fish");
word.add("chicken");
word.add("icecream");
int lengthz = word.size();

Scanner sc = new Scanner(System.in);

System.out.println("Hangman");
System.out.println("1. 1 Player");
System.out.println("2. 2 Player");
System.out.println("3. Add word");
System.out.println("4. Quit");
System.out.print("Choice : ");
int opsi = sc.nextInt();

if (opsi == 3) {
    boolean show = false;
    boolean founded = false;
    System.out.println("Input the words to be added : ");
    boolean showall = true;

    do {
        String input = sc.next() + sc.nextLine();
        for (int i = 0; i < lengthz; i++) {
            if (!input.equals(word.get(i))) {
                while (!founded) {
                    word.add(input);
                    System.out.println("Succeed!");
                    founded = true;
                }
            } else if (input.equals(word.get(i))) {
                while (!show) {
                    System.out.println("Already Added");
                    show = true;
                }
            }
        }
        System.out.println("Want to add more words?");
        String answer = sc.next() + sc.nextLine();
        if (answer.equals("no")) {
            System.out.println("Thanks for adding");
            showall = false;
        } else if (answer.equals("yes")) {
            opsi = 3;
        }
    } while (showall);
    for (int i = 0; i <= lengthz; i++) {
        System.out.println(word.get(i));
    }
}

我想要的输出就像用户想要添加更多单词“yes”然后它会重复该程序,如果用户输入“no”,那么它将显示所有单词和添加,然后返回显示菜单1-4选项。请帮助我......提前致谢!

0 个答案:

没有答案