Java 添加单词并签入Arraylist Hangman

Java 添加单词并签入Arraylist Hangman,java,loops,arraylist,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.prin

我在代码中添加单词和使用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));
    }
}
ArrayList word=new ArrayList();
添加(“鱼”);
添加(“鸡肉”);
添加(“冰淇淋”);
int lengthz=word.size();
扫描仪sc=新的扫描仪(System.in);
系统输出打印(“刽子手”);
System.out.println(“1.1播放器”);
System.out.println(“2.2播放器”);
System.out.println(“3.添加单词”);
System.out.println(“4.Quit”);
系统输出打印(“选择:”);
int opsi=sc.nextInt();
如果(opsi==3){
布尔显示=假;
布尔值=假;
System.out.println(“输入要添加的单词:”);
布尔showall=true;
做{
字符串输入=sc.next()+sc.nextLine();
对于(int i=0;i
founded = false; 
内部do-while循环

2) 对循环使用最后一次打印,如下所示:

 for(int i=0; i<word.size();i++)
 {
     System.out.println(word.get(i));
 }

for(int i=0;i构造代码,例如:

static final Scanner in = new Scanner(System.in);
static List<String> words = getStandardWords();

public static void main(String[] args) {
    while (true) {
        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 : ");
        String choice = in.nextLine();

        switch (choice) {
            case "1":
                //todo
                break;
            case "2":
                //todo
                break;
            case "3":
                addWord();
                break;
            case "4":
                System.exit(0);
                break;
            default:
                System.out.println("Invalid choice: " + choice);
                break;
        }
    }
}

static List<String> getStandardWords() {
    List<String> result = new ArrayList<>();
    result.add("fish");
    result.add("chicken");
    result.add("icecream");
    return result;
}

static void addWord() {
    System.out.println("Input the word to be added: ");
    String word = in.nextLine();

    // add word
    if (words.contains(word)) {
        System.out.println("Already Added");
    } else {
        words.add(word);
        System.out.println("Succeed!");
    }

    // add more words?
    while (true) {
        System.out.println("Want to add more words?");
        String choice = in.nextLine();

        switch (choice.toLowerCase(Locale.ROOT)) {
            case "n":
            case "no":
                System.out.println("Thanks for adding");
                // print all words
                for (int i = 0; i < words.size(); i++) {
                    System.out.println(words.get(i));
                }
                return;
            case "y":
            case "yes":
                addWord();
                return;
            default:
                System.out.println("Invalid coice: " + choice);
                break;
        }
    }
}
static final Scanner in=新扫描仪(System.in);
静态列表单词=getStandardWords();
公共静态void main(字符串[]args){
while(true){
System.out.println(“--Hangman--”);
System.out.println(“1.1播放器”);
System.out.println(“2.2播放器”);
System.out.println(“3.添加单词”);
System.out.println(“4.Quit”);
系统输出打印(“选择:”);
String choice=in.nextLine();
开关(选择){
案例“1”:
//待办事项
打破
案例“2”:
//待办事项
打破
案例“3”:
addWord();
打破
案例“4”:
系统出口(0);
打破
违约:
System.out.println(“无效选项:+选项”);
打破
}
}
}
静态列表getStandardWords(){
列表结果=新建ArrayList();
结果。添加(“鱼”);
结果:添加(“鸡肉”);
结果:添加(“冰淇淋”);
返回结果;
}
静态void addWord(){
System.out.println(“输入要添加的单词:”);
String word=in.nextLine();
//加词
if(words.contains(word)){
System.out.println(“已添加”);
}否则{
添加(word);
System.out.println(“成功!”);
}
//添加更多单词?
while(true){
System.out.println(“要添加更多单词吗?”);
String choice=in.nextLine();
开关(choice.toLowerCase(Locale.ROOT)){
案例“n”:
案例“否”:
System.out.println(“感谢添加”);
//打印所有单词
for(int i=0;i