提问人:JustNeedHelp 提问时间:7/28/2023 更新时间:7/28/2023 访问量:57
有没有一种更简单的方法可以将许多可能的参数输入到 else if 语句中?
Is there an easier way to input a lot of possible parameters into an else if statement?
问:
我正在创建一个类似于二十一个问题的游戏。要求用户想出一个词:食物、动物或物体。然后,计算机会问一堆问题,然后最终得出这个词应该是什么的结论。
目前,我的代码大部分是这样的:
//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);
}
}
它在大多数情况下工作正常,但输入所有可能的参数会让人筋疲力尽,并且需要花费大量时间。此外,仍然存在一些错误,其中包括一些问题输出为“空”,因为这些问题只是根据事先给出的答案被问到的。例如,只有当用户在询问它是否生活在陆地、天空或水中时输入“陆地”时,才会询问“它生活在山脉、平原、森林、沙漠还是城市?”这个问题。这里的问题是人们以不同的方式描述事物。如果这个词是“企鹅”,一个人可能会说水,而另一个人可能会说土地。这在猜测这个词是否是企鹅时搞砸了算法。(这有意义吗?我不知道该如何解释)
基本上,我需要找到一种方法来使代码大大缩短,同时解决可能的空问题输出。
答:
0赞
Abidstic
7/28/2023
#1
import java.util.Scanner;
public class TwentyOneQuestionsGame {
public static void main(String[] args) {
System.out.println("Think of a word: Food, Animal, or Object.");
String[] questions = {
"Size: Small, Medium, or Large?",
"Where does it live? Land, Sky, or Water?",
"If Land, does it live in Mountains, Plains, Forests, Deserts, or Cities?",
"If Sky, does it have Wings? Yes or No?",
"If Land, how many Legs does it have?",
"If Water, what type of Skin does it have?",
"Is it Dangerous? Yes or No?",
"Is it a Common Pet? Yes or No?"
};
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("land")) {
if (answers[0].equals("small")) {
if (answers[2].equals("cities")) {
if (answers[3].equals("no") && answers[4].equals("4") && answers[7].equals("yes")) {
if (answers[5].equals("fur") || answers[5].equals("hair")) {
System.out.println("It's a Cat!");
return;
}
}
} else if (answers[2].equals("cities")) {
if (answers[3].equals("no") && answers[4].equals("4") && answers[7].equals("yes")) {
if (answers[5].equals("fur") || answers[5].equals("hair")) {
System.out.println("It's a Dog!");
return;
}
}
}
}
}
System.out.println("Sorry! That's not something we recognize yet. Better luck next time!");
}
}
请尝试使用此重构代码。它可能不完全正确,但我认为你可以得到一个想法。
2赞
AztecCodes
7/28/2023
#2
是的,有一种更简单的方法! 使用地图系统应该会为您提供所需的内容。
这是我的解决方案:
动物类:
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);
}
}
主班:
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
}
}
}
}
评论