2017-08-02 30 views
-1

我是新来这个网站,以及Java。条件内条件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

你的if语句不按顺序,你缺少一个字符}关闭,如果和删除}

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

还建议:引入变量来防止重复码

String sex = sex.toLowerCase() 
String work = work.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

只是写相同的答案 - 将停止;-) –