Java 在我的情况下,当我键入一个特定单词时,如何退出程序;结束;?

Java 在我的情况下,当我键入一个特定单词时,如何退出程序;结束;?,java,chatbot,Java,Chatbot,我有个问题。我目前正在为一个聊天机器人做一个学校项目。这是聊天机器人的游戏部分。这是一个文字游戏。我可以输入错误的答案,程序会提示我重新键入答案。但是,如果我输入正确的答案,程序就停在那里,程序没有停 我想做的是重复代码,就像继续玩游戏一样,直到我输入单词“end”,但我不知道如何实现它。而且,大多数单词都在重复。例如,[animal]这个词通常会被拼成[anialm] 所以我也想得到一些关于改进代码的帮助。非常感谢你!!我还是一个刚开始学习编程的一年级学生 public static void

我有个问题。我目前正在为一个聊天机器人做一个学校项目。这是聊天机器人的游戏部分。这是一个文字游戏。我可以输入错误的答案,程序会提示我重新键入答案。但是,如果我输入正确的答案,程序就停在那里,程序没有停

我想做的是重复代码,就像继续玩游戏一样,直到我输入单词“end”,但我不知道如何实现它。而且,大多数单词都在重复。例如,[animal]这个词通常会被拼成[anialm]

所以我也想得到一些关于改进代码的帮助。非常感谢你!!我还是一个刚开始学习编程的一年级学生

public static void main(String[] args) {
        Scanner userinput = new Scanner(System.in);
        String guess = "";
        String scramble = "";
        String[] questions = {"animal", "duck", "dinosaur", "butterfly", "dog", "horse", "cow", "cat", "elephant", "crocodile", "alligator"};
        char charArr[] = null;
        String wordToGuess = questions[new Random().nextInt(questions.length)];// questions[0]

        for (int a = 0; a < wordToGuess.length(); a++) {
            charArr = wordToGuess.toCharArray();  //0a 1n 2i 3m 4a 5l 6s
            //generating a random number from the length of word to guess. Example: Animal, generated no = "1"
            int rdm = new Random().nextInt(charArr.length);
            //shuffling the words
            char temp = charArr[a];     //charArr[0] is the letter "A" which is moved to temp
            charArr[a] = charArr[rdm];  //A is then moved to charArr[rdm] which is index 1, so the word is currently "AAimals'
            charArr[rdm] = temp;        //charArr[1] contains n which is then moved to temmp, which is 0, becoming nAimal
        }
        scramble = new String(charArr);
        System.out.println("Your word is: " + scramble);

        do {
            while (!guess.equals(wordToGuess)) {
                System.out.print(">>> ");
                guess = userinput.nextLine();
                if (!guess.equals(wordToGuess)) {
                    System.out.print(">>> ");
                    System.out.println("Please try again!");
                } else {
                    System.out.println(">>> Great Job getting the answer!");
                }
            }
        }while (!guess.equalsIgnoreCase("end"));
    }
}
publicstaticvoidmain(字符串[]args){
扫描仪用户输入=新扫描仪(System.in);
字符串猜测=”;
字符串加扰=”;
字符串[]问题={“动物”、“鸭子”、“恐龙”、“蝴蝶”、“狗”、“马”、“牛”、“猫”、“大象”、“鳄鱼”、“鳄鱼”};
char charArr[]=null;
String wordToGuess=questions[new Random().nextInt(questions.length)];//questions[0]
for(int a=0;a>>”);
guess=userinput.nextLine();
如果(!guess.equals(wordToGuess)){
系统输出打印(“>>>”);
System.out.println(“请再试一次!”);
}否则{
System.out.println(“>>>很好地找到了答案!”);
}
}
}而(!guess.equalsIgnoreCase(“end”);
}
}

我建议您定义一个
布尔值
,它表示游戏的状态(是否仍在运行);然后您可以在
循环中以所述
布尔值作为条件执行所需操作

while(isRunning) {
    //actions repeated in here
}

游戏结束后,只需更改
布尔值的状态。

使用
break;
语句结束循环

if(guess.equalsIgnoreCase("end")){
      System.exit(0);
  }
if(guess.equalsIgnoreCase("end")){
     break;
  }

我认为首先要做的是重新构造代码,这将使调试更容易

我想说,您的代码基本上要求分为两部分:一部分运行游戏,另一部分包含数据:

public class Anagram {

    private String original= null;
    private String scrambled = null;

    public Anagram(String originalWord) {
        this.original = originalWord;
        this.scrambled = scramble(originalWord);
    }

    private String scramble(String original) {
        //scramble logic here
        return null;//your scrambled String
    }

    public boolean guess(String guess) {
        return original.equals(guess);
    }

}
这样我们就不用担心了,我们的运行循环基本上变成了这样:

Anagram anagram = new Anagram(wordToGuess);

String input = null;
while (true) {
    System.out.print(">>> ");
    input = userinput.nextLine();
    if (input.equalsIgnoreCase("end")) {
        System.out.println("Unlucky, please come back again");
        System.exit(0); 
    }

    if (anagram.guess(input)) {
        System.out.println(">>> Great Job getting the answer!");
        System.exit(0); 
    }
    else {
        System.out.print(">>> ");
        System.out.println("Incorrrect, please try again!");
    }
}
这使用
while(true)
,可以很容易地用其他类型的循环替换


此外,将设置代码提取到另一个方法可能会使您受益。

您还应该提到,外部循环应该通过此方法删除(并且此代码片段属于内部循环,并且在何处)。