繁体   English   中英

输入特定单词(例如“ end”)时如何退出程序?

[英]How do i exit the program when i type a specific word, in my case “end”?

我有问题 我目前正在为ChatBot做一个学校项目。 这是聊天机器人的游戏部分。 这是一个拼字游戏。 我输入的答案可能错误,程序会提示我重新输入答案。 但是,如果我输入正确的答案,程序将仅在此处停止,程序不会停止。

我想做的就是重复代码,就像继续玩游戏一样,直到我键入单词“ 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"));
    }
}

我认为要做的第一件事就是查看代码的重组,这将使调试更加容易。

我要说的是,您的代码基本上要求分成两个部分:一个用于运行游戏,另一个包含数据:

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) ,可以很容易地用其他某种循环代替。

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

使用break; 语句结束循环。

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

我建议您定义一个boolean ,它表示游戏的状态(是否仍在运行); 那么您可以将所需的操作放入以boolean条件的while循环中。

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

游戏结束后,只需更改boolean的状态即可。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM