現在、私はユーザーにジョークを知らせる簡単なプログラムを作成しています。別の "ジョーク"ステートメントの生成に関する問題
ボット:どのようにして1ドルで4つのスーツを入手できますか?あなたが:reveal
Bot:カードのデッキを購入してください:
クソクジラ
ボット:続行しますか?はい、もしくは、いいえ?
あなた:はい
ボット:戻ってこないブーメランとは何ですか?回答を表示するためにタイプを公開する
これは私の意図したスクリプトです。しかし、私は問題に直面した:
ボット:あなたは続行しますか?はい、もしくは、いいえ?
あなたは:はい
ボット:>間違い:D
ボットプリントアウト "間違った" の代わりに別のジョークを生成します。ここに私のコードです:
public class NewClass {
public static void main(String[] args) {
int randomNumber;
String userInput;
String[] jokesAnswer = {"Snowballs! HAHA ", "A stick!!! :D", "A pork chop.", "A piano! HEHE ", "A dead centipede!! xD", "At the crystal ball. *chuckle chuckle*",
"A conversation. TEEHEE", "Buy a deck of cards. *giggle giggle*"};
String[] jokes = {"What is the difference between a snowman and a snowwoman?", "What do you call a boomerang that won't come back?",
"What do you call a pig that does karate?", "What has a lot of keys but can not open any doors?", "What lies on its back, one hundred feet in the air?",
"Where do fortune tellers dance?", "What can you hold without ever touching it?", "How can you get four suits for a dollar?"};
randomNumber = (int) (Math.random() * jokes.length);
System.out.println("Bot: " + jokes[randomNumber] + " Type reveal to view answer");
Scanner input = new Scanner(System.in);
do {
System.out.print("You: ");
userInput = input.nextLine();
userInput = userInput.toLowerCase();
if (userInput.equals("reveal")) {
System.out.println("Bot: " + jokesAnswer[randomNumber] + "\nBot: Do you want to hear another joke? :D Yes or No?");
} else {
System.out.println("Bot: Wrong! >:D");
}
} while (!userInput.equals("no"));
}
ロジックが少しばらつきました。 userInputは、doループ内でのみ出力され、そこで出力されます。したがって、入力されていなくても、常に最初は「間違っています」。 –
手作業でコードをステップ実行します。あなたは同じ結果を得るでしょう。便利で一般的な構造は 'x = someMethod(); while(x!= y){process(x); x = someMethod(); } ' – slim
コードをデバッグすれば、何がうまくいかなかったのか分かります。 –