mardi 22 août 2017

JAVA while loops don't seem to be working?

So i'm trying to create this program that is essentially a dialog between the computer and the user. My while loops don't seem to be functioning. I thought maybe it could be because I have the brackets in the wrong space? Or maybe the boolean conditions I have set for the while loops are not activating? Any help would be hugely appreciated. It should return back to my topics menu if the user doesn't a) enter anything b) does not enter a correct topic c) enters a correct topic, the computer asks a relevant question then returns back to the topic menu...

this is my code:

import javax.swing.*;

public class letsstartatthebasiclevel {
    public static void main(String[] args) {

    // these are my topics / keywords
    String keyword1 = "dungeons";
    String keyword2 = "dragons";
    String keyword3 = "work";
    String keyword4 = "romance";
    String keyword5 = "books";
    String keyword6 = "gardening";
    String keyword7 = "politics";
    String keyword8 = "games";
    String keyword9 = "music";
    String keyword10 = "partying";  //conversation example. Jeeves: have you been to any good parties recently? user answers. Jeeves: thats nice to hear.

    JOptionPane.showInputDialog("Greetings traveller, I am Jeeves");

    String first_name;
    first_name = JOptionPane.showInputDialog("I hope you're well. What is your name?");

    boolean finished = false;
    while (!finished) {
        if (first_name.isEmpty()) {

            first_name = JOptionPane
                    .showInputDialog("I'm sorry I didn't quite catch that. What was your name again?");
        } else
            finished = true;

    }

    // while (first_name.equals(null)) {
    // JOptionPane.showInputDialog("I'm sorry I didn't quite catch that. What was
    // your name again?");

    // }

    JOptionPane.showMessageDialog(null, "Nice to meet you " + first_name);

    JOptionPane.showMessageDialog(null,
            "We can talk about many things, just a moment... I have a list of some topics we could talk about.. where was it..");

    String topics;

    topics = "\ndungeons\ndragons\nwork\nromance\nbooks\ngardening\npolitics\ngames\nmusic\npartying";

    String first_question = JOptionPane.showInputDialog(
            "Ah! Here it is. Is there anything here that tickles your fancy? Just enter the topic you would like to talk about"
                    + topics);

    // create a boolean to make the condition in which the following IF statement
    // will work. boolean answer_dungeons;
    // if answer contains dungeons, then tell the user about dungeons, and return to
    // the topic menu. complete this 10 times for all of the possible topics

    boolean no_topic = false;
    while (!no_topic) {  //it might be better to swap this while loop so that insted of acting on null entry, it acts on keyword entries.
        // so while keyword is detected, then ask a question IF (what keyword is entered), else "i dont know that topic message"
        if (first_question.isEmpty()) {

            first_question = JOptionPane.showInputDialog("It would be great if we both gave this conversation our %110. Just tell me which topic it is you would like to discuss." + topics );
        }
        else no_topic = true;
    }
        boolean topic_menu = false; 
                while(!topic_menu) {

        if (!first_question.contains(keyword1) 
                || !first_question.equalsIgnoreCase(keyword2)
                || !first_question.equalsIgnoreCase(keyword3)
                || !first_question.equalsIgnoreCase(keyword4)
                || !first_question.equalsIgnoreCase(keyword5)
                || !first_question.equalsIgnoreCase(keyword6)
                || !first_question.equalsIgnoreCase(keyword7)
                || !first_question.equalsIgnoreCase(keyword8)
                || !first_question.equalsIgnoreCase(keyword9)
                || !first_question.equalsIgnoreCase(keyword10)) {
            String question_loop = JOptionPane.showInputDialog("It would be great if we both gave this conversation our %110. Just tell me which topic it is you would like to discuss." + topics );
        }
        else topic_menu = true;

            if (first_question.equalsIgnoreCase(keyword1)) {
                JOptionPane.showMessageDialog(null, "Ah dungeons is your pick.. Clever.\nIt is rumored that some bandits are camping in the caverns to the northeast of here...\n\nMaybe what you seek lies there.\nClick ok and we will return to our list of topics.");
                topic_menu = true;
            } 
                else 
                    if (first_question.equalsIgnoreCase(keyword2)) {
                        JOptionPane.showMessageDialog(null, "Giant flying beasts is your interest, well there has been talk of dragons breathing fire on villagers to the south.\n I wouldn't risk my life for the thrill of seeing a dragon.\nYou may like to pay them a visit though... Good luck!");
                    }
                    else 
                        if(first_question.equalsIgnoreCase(keyword3)) {
                            JOptionPane.showInputDialog("How is work going for you?");
                            JOptionPane.showMessageDialog(null, "Dream big, work hard, stay focussed, and surround yourself with good people.");
                        }
                        else 
                            if(first_question.equalsIgnoreCase(keyword4)) {
                                JOptionPane.showInputDialog("Is there a lover on the horizon? How is your love life?");
                                JOptionPane.showMessageDialog(null, "You wanna know who I'm in love with?\nRead the first two words again.");
                            }
                            else
                                if(first_question.equalsIgnoreCase(keyword5)) {
                                    JOptionPane.showInputDialog("Read any good books recently?");
                                    JOptionPane.showMessageDialog(null, "Keep up the reading, A book is a gift you can open again and again");
                                }
                                else
                                    if(first_question.equalsIgnoreCase(keyword6)) {
                                        JOptionPane.showInputDialog("Gardening, what a lovely topic!");
                                        JOptionPane.showMessageDialog(null, "To plant a garden is to believe in tomorrow!");
                                    }
                                    else 
                                        if(first_question.equalsIgnoreCase(keyword7)) {
                                            JOptionPane.showMessageDialog(null, "Make sure you're enrolled to vote for the Marraige Equality postal plebiscite");
                                        }
                                        else
                                            if(first_question.equalsIgnoreCase(keyword8)) {
                                                JOptionPane.showInputDialog("Played any good games recently?");
                                                JOptionPane.showMessageDialog(null, "Remember to take things in moderation. Even video games!");
                                            }
                                            else
                                                if(first_question.equalsIgnoreCase(keyword9)) {
                                                    JOptionPane.showInputDialog("Heard any good music lately?");
                                                    JOptionPane.showMessageDialog(null, "Music gives a soul to the universe, wings to the mind, flight to the imagination,\nand life to everything.");
                                                }
                                                else 
                                                    if(first_question.equalsIgnoreCase(keyword10)) {
                                                        JOptionPane.showInputDialog("Ah.. good old partying.\nWhat good parties have you been to recently?");
                                                        JOptionPane.showMessageDialog(null, "I don't drink.\nI don't smoke.\nI don't party.");
                                                    }
                                                    else topic_menu = true;


                                                        }


         topic_menu = false;}
        }
            //this does not currently work as the loop isnt functioning as it should. 

Aucun commentaire:

Enregistrer un commentaire