I'm having a problem. I'm currently doing a school project for a ChatBot. This is a game section for the chatbot. It's a word scramble game. I can enter the wrong answer and the program would prompt me to retype the answer. But, if I type the correct answer, the program just stops there, the program didn't stop.
What I would like to do is to repeat the code, like keeping playing the game, until I type the word "end", but I don't know how to implement it. Also, most of the words kept repeating. For example, the word [animal] would usually be scrambled into [anialm].
So I would like to get some help on improving the code too. Thank you very much!! I am still a Year 1 who just started learning to program.
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"));
}
}