Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there an easier way to input a lot of possible parameters into an else if statement?

I’m creating a game kind of like twenty one questions. The user is asked to think of a word: Food, Animal, or Object. The computer then asks a bunch of questions before eventually coming to the conclusion of what the word should be.

Currently, I’ve got the code for the most part worked out like this:

 //Guesses the animal based on given answers
        if (a1.equals("Animal")) {
            if (aA1.equals("Small") && aAs1.equals("Small") && aA2.equals("Land") && aA3.equals("No") && aA4 == 4 && (aAld1.equals("Plains") | aAld1.equals("Cities")) && (aA5.equals("Fur") | aA5.equals("Hair")) && aA6.equals("Yes") && (aAld2.equals("No"))) {
                System.out.println("Mouse!");
                /*
                Size: Small, Small
                Domain: Land
                Wings: No
                Legs: 4
                Home: Plains | Cities
                Skin Type: Fur | Hair
                Common Pet: Yes
                Domesticated: No
                 */
            } else if (aA1.equals("Small") && aAs1.equals("Small") && aA2.equals("Water") && aAw1.equals("Most") && aA3.equals("No") && aA4 == 0  && aA5.equals("Scales") && aA6.equals("Yes")) {
                System.out.println("Fish!");
                /*
                Size: Small, Small
                Domain: Water
                Body of Water: Most
                Wings: No
                Legs: 0
                Skin Types: Scales
                Common Pet: Yes
                 */
            } else if (aA1.equals("Large") && aA2.equals("Water") && (aAw1.equals("Oceans") | aAw1.equals("Seas")) && aA3.equals("No") && aA4 == 0  && aA5.equals("Skin") && aAl1.equals("Dangerous") && aA6.equals("No")) {
                System.out.println("Shark!");
                /*
                Size: Large
                Domain: Water
                Body of Water: Oceans | Seas
                Wings: No
                Legs: 0
                Skin Type: Skin
                Danger Level: Dangerous
                Common Pet: No
                 */
            } else if ((aA1.equals("Small") | aA1.equals("Medium")) && (aAs1.equals("Small") | aAs1 != "Small") && (aA2.equals("Land")) && (aA3.equals("No")) && aA4 == 4 && (aAld1.equals("Cities")) && aAld2.equals("Yes") && aA5.equals("Fur") && aA6.equals("Yes") && aAfc.equals("Feline")) {
                System.out.println("Cat!");
                /*
                Size: Small | Medium, Small | !Small
                Domain: Land
                Wings: No
                Legs: 4
                Home: Cities
                Domesticated: Yes
                Skin Type: Fur
                Common Pet: Yes
                 */
            } else if ((aA1.equals("Small") | aA1.equals("Medium")) && (aAs1.equals("Small") | aAs1 != "Small") && aA2.equals("Land") && aA3.equals("No") && aA4 == 4 && aAld1.equals("Cities") && aAld2.equals("Yes") && aA5.equals("Fur") && aA6.equals("Yes") && aAfc.equals("Canine")) {
                System.out.println("Dog!");
                /*
                Size: Small | Medium, Small | !Small
                Domain: Land
                Wings: No
                Legs: 4
                Home: Cities
                Domesticated: Yes
                Skin Type: Fur
                Common Pet: Yes
                 */
            }else {
                System.out.println("Sorry! That's not an animal we recognize yet. Better luck next time!");
                System.exit(1);
            }
        }

It works fine for the most part, but entering in all of the possible parameters is exhausting, and takes a ridiculous amount of time. Plus, there’s still some bugs, which include some questions outputs being "null" due to the fact that they’re only asked based on the answer given beforehand. For example, the question "Does it live in the Mountains, Plains, Forests, Deserts, or Cities?" is only asked if the user enters "Land" when asking if it lives on Land, in the Sky, or in the Water. The problem here is that people describe things differently. If the word was "penguin", one person might say water, while another might say land. This messes up the algorithm when guessing if the word is penguin. (Does this make sense? I’m not sure how else to explain it)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Basically, I need to find a way to make the code considerably shorter while also solving the possibly null question output.

>Solution :

Yes there is an easier way!
Using a Map System should give you what you want.

Here is my solution:

Animal Class:

public class Animal {

    // Map variable gets created here
    Map<String, String> properties = new HashMap<>();

    // Constructor
    public Animal(String size, String domain, String wings, int legs, String home, String skinType, String commonPet, String domesticated) {
        properties.put("Size", size);
        properties.put("Domain", domain);
        properties.put("Wings", wings);
        properties.put("Legs", String.valueOf(legs));
        properties.put("Home", home);
        properties.put("SkinType", skinType);
        properties.put("CommonPet", commonPet);
        properties.put("Domesticated", domesticated);
    }
}

Main Class:

public class Main {
    public static void main(String[] args) {
        // Variables of the animals. Here you use the Animals classes constructor to fill the Animal Class Object attributes with values
        Animal mouse = new Animal("Small", "Land", "No", 4, "Plains,Cities", "Fur,Hair", "Yes", "No");
        Animal fish = new Animal("Small", "Water", "No", 0, null, "Scales", "Yes", null);
        // Here you can add all the animals!

        List<Animal> animals = new ArrayList<>(Arrays.asList(mouse, fish /*, other animals... */));

        // foreach loop iterating thorugh the animals List
        for (Animal animal : animals) {
            if (animal.properties.get("Size").equals(aA1) /* && other conditions... */ ) {
                // Output: Animal Name
            }
        }
    }
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading