条件java中的条件

时间:2017-08-02 08:00:14

标签: java variables if-statement chatbot

我是这个网站的新手,也是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;
        }
}

2 个答案:

答案 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 :(得分:1)

您的if语句不是有序的,您缺少一个字符}以关闭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()

此外,您可以将小写值中的toLowerCase()移除为"story".toLowerCase()