dimanche 18 mars 2018

When ever a question mark is imputed by a user it will display a string

Okay so basically iv been working on my school assignment which happens to be creating a very simple ChatterBot with java using Eclipse. So far iv gotten it to ask a series of questions if the user input contained a certain word, but i also need make it so when ever a "?" is entered (no matter at what point, it will probs make sense when you look at the code) it will display the string " i will be asking the questions", so far iv only got it to work only for the first time a user makes an input, then it goes to the next question and if you type a "?" nothing happens and the program ends.

import javax.swing.JOptionPane;
public class ChatterBot {
public static void main(String args[]) {
    String input = "";
    String maths = "";
    String science = "";
    String chemFact = "";
    String bioFact = "";
    String zooFact = "";
    String algFact = "";
    String yes = "";
    String no = "";
    input = JOptionPane
            .showInputDialog("Pick one of the subjects listed to learn a fun fact (english, science, maths) ");

    if (input.contains("science")) {
        science = JOptionPane.showInputDialog(
                "What kind of science fact woukd you like to know about? (chem, Biology, Zoology)");
    }

    else if (input.contains("maths")) {
        maths = JOptionPane.showInputDialog(
                "What kind of maths fact would you like to know about? (algebra, fractions, division) ");
    }
    if (maths.contains("algebra")) {
        algFact = JOptionPane.showInputDialog(
                "\"Did you know a mathematician who specializes in algebra is called an algebraist? (yes or no)\"");
    }
    if (algFact.contains("yes")) {
        System.out.println("Well good for you");
    } else if (algFact.contains("no")) {
        System.out.println("You learn something new everyday :)");
    }

    if (science.contains("chem")) {
        chemFact = JOptionPane.showInputDialog(
                "Did you know If you pour a handful of salt into a full glass of water the water level will actually go down rather than overflowing the glass? (yes or no)");
    }

    if (chemFact.contains("yes")) {
        System.out.println("Well good for you");
    } else if (chemFact.contains("no")) {
        System.out.println("You learn something new everyday :)");
    }

    else if (science.contains("biology")) {
        bioFact = JOptionPane.showInputDialog("Did you know The brain itself cannot feel pain? (yes or no)");
    }

    if (bioFact.contains("yes")) {
        System.out.println("Well good for you");
    } else if (bioFact.contains("no")) {
        System.out.println("You learn something new everyday :)");
    }

    else if (science.contains("zoology")) {
        zooFact = JOptionPane
                .showInputDialog("Did you know butterflies have taste receptors on their feet? (yes or no)");
    }

    if (zooFact.contains("yes")) {
        System.out.println("Well good for you");
    } else if (zooFact.contains("no")) {
        System.out.println("You learn something new everyday :)");
    }
    if (input.contains("?")) {
        System.out.println("I will be asking the questions");
    }

}

}

Aucun commentaire:

Enregistrer un commentaire