問題が発生しています。私は現在、チャットボットのための学校プロジェクトをやっています。これはチャットボットのゲームセクションです。それは単語スクランブルゲームです。私は間違った答えを入力することができ、プログラムは答えを再入力するよう促します。しかし、私が正解を入力すると、プログラムはそこで停止し、プログラムは停止しませんでした。私は特定の単語を入力すると、私の場合 "終了"でプログラムを終了するにはどうすればいいですか?
私がしたいのは、単語 "end"を入力するまで、ゲームを続けるようなコードを繰り返すことですが、実装方法はわかりません。また、言葉のほとんどは繰り返していました。例えば、[動物]という言葉は、通常、[anialm]にスクランブルされます。
私はコードを改善するための助けも受けたいと思います。どうもありがとうございました!!私はまだプログラムを学ぶことを始めたばかりの1年生です。
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"));
}
}
また、外側のループはこれで削除する必要があります(このコードフラグメントは内部ループに属しています)。 – Njol