mercredi 2 août 2017

Condition within condition java

I'm new to this website and as well as java.

Can anyone help me find out why some part of the program is not working even though there's no red dots?

I've put comment using // on the lines not working.

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

Aucun commentaire:

Enregistrer un commentaire