2017-07-15 5 views
-1

質問と回答を作成して2つの配列に格納することができる単純なチャットボットを作成する作業が行われています。したがって、ユーザが同じ質問を入力するたびに、チャットボットは回答を印刷することができます。#Javaヘルプ。

たとえば、私の意図する実行は: Bot:作成したい第1の質問です。 ユーザー:私の名前は何ですか? Bot:レスポンスは? ユーザー:Tom Bot:作成する2番目の質問。 ユーザー:どのように高さですか? Bot:レスポンスは? あなた:179cm あなた:私はどのくらいの高さですか? Bot:179cm あなた:私の名前は何ですか? ボット:

パブリック静的無効メイン(文字列[] args){

 String reply = ""; 
     String[] storeQuestions = new String [100]; 
     String[] storeAnswers = new String [100]; 

     do { 
      /* first question*/ 
      System.out.println("Bot: 1st question that you want to create."); 
      Scanner input = new Scanner (System.in); 
      System.out.print("You: "); 
      String createQuestion = input.nextLine(); // change to string 
      System.out.println("Bot: and the response is?"); 
      System.out.print("You: "); 
      String answer = input.nextLine(); 

      /* storing question and answer into the two arrays */ 
      storeQuestions[0] = createQuestion; 
      storeAnswers[0] = answer; 

      /* second question */ 
      System.out.println("Bot: 2nd question that you want to create."); 
      System.out.print("You: "); 
      createQuestion = input.nextLine(); 
      System.out.println("Bot: and the response is?"); 
      System.out.print("You: "); 
      answer = input.nextLine(); 

      storeQuestions[1]= createQuestion; 
      storeAnswers[1] = answer; 
      System.out.print("You: "); 
      reply = input.nextLine(); 

      if(storeQuestions[0]==createQuestion) { 
       System.out.println("Bot: "+storeAnswers[0]); 
      } 
      else if (storeQuestions[1]==createQuestion) { 
       System.out.println("Bot: "+storeAnswers[1]); 
      } 
     }while(reply!="bye");    
}  

} ==オペレータがのハッシュコードを比較

+1

'文字列を比較するために'等号(文字列他)を使用します。それ以外に、あなたが直面している問題が何であるかは明らかではありません。 –

答えて

-1

:トム

は、以下の私のソースコードであります2つのオブジェクト。 2つの文字列が同じではないかもしれないので(文字は同じですが)、文字ごとにチェックする必要があります。これを行うための組み込みメソッドはString.equals()です。大文字と小文字が関係ない場合は、String.equalsIgnoreCase()を使用します。

例:

"test".equals("test")   true 
"TEST".equals("test")   false 
"TEST".equalsIgnoreCase("test") true