2017-08-02 19 views
-1

私はこのウェブサイトだけでなく、javaです。条件内の条件

赤い点がなくても、プログラムの一部が動作しない理由を知ることができますか?

私は、動作しない行で//を使用してコメントを挿入しました。

import javax.swing.JOptionPane; 

public class ChatterBot { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    String firstName, work, sex = null; 
    int age; 
    firstName = JOptionPane.showInputDialog("Hello, my name is Chatterbox. What is your name?"); 
    if (firstName.toLowerCase().contains("name".toLowerCase())) { 
     JOptionPane.showMessageDialog(null, "Welcome " + firstName.substring(firstName.lastIndexOf(" ") + 1) + "!"); 
     sex = JOptionPane.showInputDialog(null, "Is " + firstName.substring(firstName.lastIndexOf(" ") + 1) 
       + " a guy name or a woman name? (type stop to end conversation)"); 
    } else { 
     JOptionPane.showMessageDialog(null, "Welcome " + firstName + "!"); 
     sex = JOptionPane.showInputDialog(null, 
       "Is " + firstName + " a guy name or a woman name (type stop to end conversation)"); 
    } 

    while (true) 
     if (sex.toLowerCase().contains("guy".toLowerCase())) { 
      JOptionPane.showMessageDialog(null, "Welcome friend"); 
      work = JOptionPane 
        .showInputDialog("Would you like to talk about work or do you want to hear a cool story?"); 
      if (work.toLowerCase().contains("work".toLowerCase())) { 
       JOptionPane.showMessageDialog(null, "Interesting"); 
       break; 

      } else if (work.toLowerCase().contains("story".toLowerCase())) { 
       JOptionPane.showMessageDialog(null, "hola"); 
       break; 
      } else if (sex.toLowerCase().contains("woman".toLowerCase())) { 
//when I type woman nothing happens but the else if below for "stop" works. 
       age = Integer.parseInt(JOptionPane.showInputDialog(null, "How old are you?")); 
       if (age >= 18 && age <= 40) { 
        JOptionPane.showMessageDialog(null, "Dayummm"); 
       } else if (age > 40) { 
        JOptionPane.showMessageDialog(null, "I don't like no cougar!"); 
       } else { 
        JOptionPane.showMessageDialog(null, "I ain't no pedo. Bye!"); 
       } 
      } 

      break; 
     } else if (sex.toLowerCase().contains("stop".toLowerCase())) { 
      JOptionPane.showMessageDialog(null, "Have a nice day."); 
      break; 
     } else { 
      JOptionPane.showMessageDialog(null, "Goodbye"); 
      break; 
     } 
} 
+0

デバッガは申し訳ありません – Jens

+0

あなたを助けることができるが、これはサービス「私たちはあなたのためにあなたのプログラムをデバッグ」ではありません。そして、あなたは何が助けられるかを知っています:*新しい場所に参加するときは、最初にその場所のルールについて学んでください。盲目的にコンテンツを投棄するのではありません。その意味で:A)この "質問"を削除してくださいB)[ヘルプ]を読んでより良い質問をする方法を学んでください;-) – GhostCat

+0

ようこそ!デバッグの助けを求める質問(「なぜこのコードは動作しませんか?」)には、目的の動作、特定の問題またはエラー、および質問自体の中でそれを再現するのに必要な最短コードが含まれていなければなりません。明確な問題文がない質問は、他の読者にとって有用ではありません。参照:[mcve]を作成する方法。あなたの*質問を改善するために "編集"リンクを使用してください - コメントでより多くの情報を追加しないでください。ありがとう! – GhostCat

答えて

1

文は順番になっていない場合はあなた、あなたが提案場合は閉じて、また}

} else if (sex.toLowerCase().contains("woman".toLowerCase())) { 
//when I type woman nothing happens but the else if below for "stop" works. 
       age = Integer.parseInt(JOptionPane.showInputDialog(null, "How old are you?")); 
       if (age >= 18 && age <= 40) { 
        JOptionPane.showMessageDialog(null, "Dayummm"); 
       } else if (age > 40) { 
        JOptionPane.showMessageDialog(null, "I don't like no cougar!"); 
       } else { 
        JOptionPane.showMessageDialog(null, "I ain't no pedo. Bye!"); 
       } 


      break; 
     } 

を削除するには1つの文字}が欠落している:重複コード

String sex = sex.toLowerCase() 
String work = work.toLowerCase() 
を防ぐために、変数を導入します

さらに小文字の値のtoLowerCase()を削除するには、"story".toLowerCase()

+0

私はコンパイルエラーを書きませんでした。単にifステートメントを修正しました。私は詳細を書くでしょう – user7294900

+0

申し訳ありませんあなたの文言はわかりませんでした –

2

あなたif (sex.toLowerCase().contains("woman".toLowerCase()))ブロックがif (sex.toLowerCase().contains("guy".toLowerCase()))ブロック内にネストされていますが、彼らは同じレベルにする必要があります:

if (sex.toLowerCase().contains("guy".toLowerCase())) { 
     JOptionPane.showMessageDialog(null, "Welcome friend"); 
     work = JOptionPane 
       .showInputDialog("Would you like to talk about work or do you want to hear a cool story?"); 
     if (work.toLowerCase().contains("work".toLowerCase())) { 
      JOptionPane.showMessageDialog(null, "Interesting"); 
      break; 

     } else if (work.toLowerCase().contains("story".toLowerCase())) { 
      JOptionPane.showMessageDialog(null, "hola"); 
      break; 
     } 

     break; 
    } else if (sex.toLowerCase().contains("woman".toLowerCase())) { 
      //when I type woman nothing happens but the else if below for "stop" works. 
      age = Integer.parseInt(JOptionPane.showInputDialog(null, "How old are you?")); 
      if (age >= 18 && age <= 40) { 
       JOptionPane.showMessageDialog(null, "Dayummm"); 
      } else if (age > 40) { 
       JOptionPane.showMessageDialog(null, "I don't like no cougar!"); 
      } else { 
       JOptionPane.showMessageDialog(null, "I ain't no pedo. Bye!"); 
      } 
    } else if (sex.toLowerCase().contains("stop".toLowerCase())) { 
     JOptionPane.showMessageDialog(null, "Have a nice day."); 
     break; 
    } else { 
     JOptionPane.showMessageDialog(null, "Goodbye"); 
     break; 
    } 
+1

同じ答えを書くだけで - ;-)停止します –