2017-07-25 6 views
1

現在、私はユーザーにジョークを知らせる簡単なプログラムを作成しています。別の "ジョーク"ステートメントの生成に関する問題

ボット:どのようにして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")); 

    } 
+0

ロジックが少しばらつきました。 userInputは、doループ内でのみ出力され、そこで出力されます。したがって、入力されていなくても、常に最初は「間違っています」。 –

+0

手作業でコードをステップ実行します。あなたは同じ結果を得るでしょう。便利で一般的な構造は 'x = someMethod(); while(x!= y){process(x); x = someMethod(); } ' – slim

+0

コードをデバッグすれば、何がうまくいかなかったのか分かります。 –

答えて

0

あなたのロジックは少し壊れています。あなたはdo-whileループの中で冗談を言う必要があります。そうでなければ、一度だけそれを伝えます。あなたは「公開」以外の何かを何度も入力する状況に対処できる必要があるので、私はそのケースのために別のループを追加しました。それはまったく起こることが保証される必要がないので、do-whileとは対照的にwhileループです。

Scanner input = new Scanner(System.in); 

do { 
    randomNumber = (int) (Math.random() * jokes.length); 
    System.out.println("Bot: " + jokes[randomNumber] + " Type reveal to view answer"); 

    System.out.print("You: "); 
    userInput = input.nextLine(); 
    userInput = userInput.toLowerCase(); 

    while (!userInput.equals("reveal")) 
    { 
     System.out.println("Bot: Wrong! >:D"); 

     System.out.print("You: "); 
     userInput = input.nextLine(); 
     userInput = userInput.toLowerCase(); 
    } 

    System.out.println("Bot: " + jokesAnswer[randomNumber] + "\nBot: Do you want to hear another joke? :D Yes or No?"); 

    System.out.print("You: "); 
    userInput = input.nextLine(); 
    userInput = userInput.toLowerCase(); 

} while (!userInput.equals("no")); 

コードの重複を避けるために、ユーザー入力ステートメントを独自の関数に移動します。

1

あなたのコードの失敗の理由は、あなたのロジックは、ユーザの入力にすべての可能なシナリオをcevringされていないということです。これは

(!userInput.equals("no")); 

を失敗するまで、あなたは、whileループでありますしかし、ループ内で、あなたはこの

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"); 
} 

ので、基本的には同じポイントにあなたをもたらす「明らか」ではありません何も...

012を持っています

ソリューション:

あなたはあなたが実際にループ内で、さらにユーザーに入力を求めるプロンプトが表示されない

else if (userInput.equals("yes")) { 
     System.out.println("yes it is"); 
} else { 
     System.out.println("Bot: Wrong! >:D"); 
} 
+1

'equals'の代わりに' equalsIgnoreCase'を使うこともできます。 – Arpit

+1

@Arpit 'userInput'はすでに小文字に変換されています。 – 4castle

+0

@ 4キャスル私の悪い:( – Arpit

0

オプションを展開でき、ユーザーが実際に質問に応答するための方法はありません、また実際にはもっとジョークを伝えるための仕組みがあるのです。そのロジックはすべてループの外にあるので、再び起こることはありません。正しいisn't

0

あなたDo-While-Loop、I'dはあなたに次のような提案:あなたが書いた「明らかに」とそれなしに行くcan't、しかし - 私するためにボットが待機する

注意を必ずポイントを確認し、必要に応じてコードを変更することができます。

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?"}; 

    do{ 
     randomNumber = (int) (Math.random() * jokes.length); 
     System.out.println("Bot: " + jokes[randomNumber] + " Type reveal to view answer"); 
     Scanner input = new Scanner(System.in);    

     do{    
      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("reveal")) 

     userInput = input.nextLine(); 
     userInput = userInput.toLowerCase(); 
    }while(!userInput.equals("no")) 
} 

(お気軽にない場合)は userInput != "reveal"だけでは、次のとインナー do-while-loopを置き換えることができます継続するかどうか尋ねるしたいときに冗談をreavealないようにしたい場合:

userInput = input.nextLine(); 
userInput = userInput.toLowerCase(); 

if (userInput.equals("reveal")) { 
    System.out.println("Bot: " + jokesAnswer[randomNumber]); 
} else { 
    System.out.println("Bot: Wrong! >:D"); 
} 

System.out.println("Bot: Do you want to hear another joke? :D Yes or No?"); 

この場合、ボットがジョークを明らかにするかどうかにかかわらず、「間違っている」と言われても、続行するかどうか尋ねられます。

+1

ああ、スキャナに慣れていないが、私は答えを編集するつもりだ – FatTony

+0

だからあなたのコメントを削除しています。 – Michael

関連する問題