有没有更简单的方法将许多可能的参数输入到 else if 语句中?

huangapple go评论73阅读模式
英文:

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

问题

我正在创建一款类似于21个问题的游戏。用户被要求想一个词:食物、动物或物体。然后计算机会在最终得出答案之前提出一堆问题。

目前,我已经有了大部分的代码,像这样:

// 根据给定的答案猜测动物
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!");
        /*
        大小:小,小
        领域:陆地
        翅膀:否
        腿:4
        家:平原 | 城市
        皮肤类型:毛皮 | 毛发
        常见宠物:是
        驯化:否
         */
    } 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!");
        /*
        大小:小,小
        领域:水
        水体:大多数
        翅膀:否
        腿:0
        皮肤类型:鳞片
        常见宠物:是
         */
    } 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!");
        /*
        大小:大
        领域:水
        水域:海洋 | 海洋
        翅膀:否
        腿:0
        皮肤类型:皮肤
        危险级别:危险
        常见宠物:否
         */
    } else if ((aA1.equals("Small") || aA1.equals("Medium")) && (aAs1.equals("Small") || !aAs1.equals("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!");
        /*
        大小:小 | 中等,小 | !小
        领域:陆地
        翅膀:否
        腿:4
        家:城市
        驯化:是
        皮肤类型:毛皮
        常见宠物:是
         */
    } else if ((aA1.equals("Small") || aA1.equals("Medium")) && (aAs1.equals("Small") || !aAs1.equals("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!");
        /*
        大小:小 | 中等,小 | !小
        领域:陆地
        翅膀:否
        腿:4
        家:城市
        驯化:是
        皮肤类型:毛皮
        常见宠物:是
         */
    } else {
        System.out.println("对不起!这不是我们认识的动物。祝你下次好运!");
        System.exit(1);
    }
}

这个代码大部分运行良好,但是输入所有可能的参数非常繁琐,而且需要很长的时间。而且,仍然存在一些错误,其中包括一些问题的输出为“null”,因为它们仅根据先前给出的答案提出。例如,问题“它是否生活在山地、平原、森林、沙漠或城市中?”只有在用户在询问是否生活在陆地、天空或水中时输入“Land”时才会被问到。这里的问题是人们对事物的描述不同。如果词是“企鹅”,一个人可能会说水,而另一个人可能会说陆地。这会使算法在猜测这个词是否是企鹅时出错。(这有意义吗?我不确定如何用其他方式解释它)

基本上,我需要找到一种方法,使代码变得更短,同时解决可能为null的问题输出。

英文:

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)

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

答案1

得分: 2

是的,有一种更简单的方法!
使用地图系统应该可以满足您的要求。

以下是我的解决方案:

动物类:

public class Animal {

    // 在这里创建地图变量
    Map<String, String> properties = new HashMap<>();

    // 构造函数
    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);
    }
}

主类:

public class Main {
    public static void main(String[] args) {
        // 动物的变量。在这里,您使用动物类的构造函数来填充动物类对象的属性值
        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);
        // 在这里,您可以添加所有的动物!

        List<Animal> animals = new ArrayList<>(Arrays.asList(mouse, fish /*, 其他动物... */));

        // foreach循环遍历动物列表
        for (Animal animal : animals) {
            if (animal.properties.get("Size").equals(aA1) /* && 其他条件... */) {
                // 输出:动物名称
            }
        }
    }
}

如果需要进一步的帮助,请随时告诉我。

英文:

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&lt;String, String&gt; properties = new HashMap&lt;&gt;();

    // Constructor
    public Animal(String size, String domain, String wings, int legs, String home, String skinType, String commonPet, String domesticated) {
        properties.put(&quot;Size&quot;, size);
        properties.put(&quot;Domain&quot;, domain);
        properties.put(&quot;Wings&quot;, wings);
        properties.put(&quot;Legs&quot;, String.valueOf(legs));
        properties.put(&quot;Home&quot;, home);
        properties.put(&quot;SkinType&quot;, skinType);
        properties.put(&quot;CommonPet&quot;, commonPet);
        properties.put(&quot;Domesticated&quot;, 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(&quot;Small&quot;, &quot;Land&quot;, &quot;No&quot;, 4, &quot;Plains,Cities&quot;, &quot;Fur,Hair&quot;, &quot;Yes&quot;, &quot;No&quot;);
        Animal fish = new Animal(&quot;Small&quot;, &quot;Water&quot;, &quot;No&quot;, 0, null, &quot;Scales&quot;, &quot;Yes&quot;, null);
        // Here you can add all the animals!

        List&lt;Animal&gt; animals = new ArrayList&lt;&gt;(Arrays.asList(mouse, fish /*, other animals... */));

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

答案2

得分: 0

import java.util.Scanner;

public class TwentyOneQuestionsGame {
    public static void main(String[] args) {
        System.out.println("想一个词:食物、动物还是物体?");

        String[] questions = {
            "大小:小、中、大?",
            "它生活在哪里?陆地、天空还是水中?",
            "如果是陆地,它生活在山区、平原、森林、沙漠还是城市?",
            "如果是天空,它有翅膀吗?是或否?",
            "如果是陆地,它有多少条腿?",
            "如果是水中,它的皮肤是什么样的?",
            "它危险吗?是或否?",
            "它是常见的宠物吗?是或否?"
        };

        String[] answers = new String[questions.length];
        Scanner scanner = new Scanner(System.in);

        for (int i = 0; i < questions.length; i++) {
            System.out.println(questions[i]);
            String input = scanner.nextLine();
            answers[i] = input.toLowerCase();
        }

        if (answers[1].equals("陆地")) {
            if (answers[0].equals("小")) {
                if (answers[2].equals("城市")) {
                    if (answers[3].equals("否") && answers[4].equals("4") && answers[7].equals("是")) {
                        if (answers[5].equals("毛皮") || answers[5].equals("毛")) {
                            System.out.println("它是一只猫!");
                            return;
                        }
                    }
                } else if (answers[2].equals("城市")) {
                    if (answers[3].equals("否") && answers[4].equals("4") && answers[7].equals("是")) {
                        if (answers[5].equals("毛皮") || answers[5].equals("毛")) {
                            System.out.println("它是一只狗!");
                            return;
                        }
                    }
                }
            }
        }

        System.out.println("抱歉!我们还不认识这个东西。祝下次好运!");
    }
}

这是您提供的Java代码的翻译部分。如您所请求,我只提供了代码的翻译,没有额外的内容。

英文:
    import java.util.Scanner;
public class TwentyOneQuestionsGame {
public static void main(String[] args) {
System.out.println(&quot;Think of a word: Food, Animal, or Object.&quot;);
String[] questions = {
&quot;Size: Small, Medium, or Large?&quot;,
&quot;Where does it live? Land, Sky, or Water?&quot;,
&quot;If Land, does it live in Mountains, Plains, Forests, Deserts, or Cities?&quot;,
&quot;If Sky, does it have Wings? Yes or No?&quot;,
&quot;If Land, how many Legs does it have?&quot;,
&quot;If Water, what type of Skin does it have?&quot;,
&quot;Is it Dangerous? Yes or No?&quot;,
&quot;Is it a Common Pet? Yes or No?&quot;
};
String[] answers = new String[questions.length];
Scanner scanner = new Scanner(System.in);
for (int i = 0; i &lt; questions.length; i++) {
System.out.println(questions[i]);
String input = scanner.nextLine();
answers[i] = input.toLowerCase();
}
if (answers[1].equals(&quot;land&quot;)) {
if (answers[0].equals(&quot;small&quot;)) {
if (answers[2].equals(&quot;cities&quot;)) {
if (answers[3].equals(&quot;no&quot;) &amp;&amp; answers[4].equals(&quot;4&quot;) &amp;&amp; answers[7].equals(&quot;yes&quot;)) {
if (answers[5].equals(&quot;fur&quot;) || answers[5].equals(&quot;hair&quot;)) {
System.out.println(&quot;It&#39;s a Cat!&quot;);
return;
}
}
} else if (answers[2].equals(&quot;cities&quot;)) {
if (answers[3].equals(&quot;no&quot;) &amp;&amp; answers[4].equals(&quot;4&quot;) &amp;&amp; answers[7].equals(&quot;yes&quot;)) {
if (answers[5].equals(&quot;fur&quot;) || answers[5].equals(&quot;hair&quot;)) {
System.out.println(&quot;It&#39;s a Dog!&quot;);
return;
}
}
}
}
}
System.out.println(&quot;Sorry! That&#39;s not something we recognize yet. Better luck next time!&quot;);
}
}

Try using this refactored code. It may not be fully correct but I think you can get a idea.

huangapple
  • 本文由 发表于 2023年7月28日 01:07:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定