为此,我使用了许多嵌套的if / else语句。
我有三个主要分支(活动物,有生命的植物,无生命的事物),每个分支都有多个分支。做出约60个不同的决定。
我很难让它进行合作,并控制所有if / else语句。由于需要重新启动太多,因此我没有太多代码,但目前我在:
System.out.println("Think of Something");
System.out.println("Is it a living animal, living plant, or non-living thing? ");
String user = get.nextLine();
if (user.equals("living animal")); {
//starts animal tree
System.out.println("Does it have feathers, fur, or neither?");
String user2 = get.nextLine();
if (user2.equals("feathers")); {
System.out.println("is it bigger than a soccer ball?");
}
} else if (user2.equals("fur")); {
System.out.println("is it domesticated?");
// end animal tree
} else if (user.equals("living plant")); {
// start plant tree
System.out.println("is it a tree?");
}
} // end method
} //end program
答案 0 :(得分:2)
您正在使用以下语法写出if
语句:
if (user2.equals("feathers"));
{
System.out.println("is it bigger than a soccer ball?");
}
但是,if
块的主体将始终执行,因为您有一个分号会提前完成语句:
if (user2.equals("feathers")); // <-- The semicolon here finishes the if statement, which means the if statement does nothing
{
System.out.println("is it bigger than a soccer ball?"); // <-- This line is ran no matter what the if statement was
}
要使if
或else if
语句正常工作,您要做的基本上就是删除不需要的分号。
答案 1 :(得分:1)
作为解决该问题的一个示例,该问题变得非常复杂,无法解决。并不是要立即可用的现成程序。
回答“在进行协作时有很多麻烦并控制所有if / else语句时”该如何简化事情的问题。如果需要,可以针对此类情况采取策略。
我也为示范作了一些夸大的事。在实践中,您可以执行似乎很方便的操作。另外:为了简单起见,我将所有内容都设为静态-在扩展的应用程序中,您肯定会使用实例。
步骤1:您从一个非常简单的类框架开始。简洁是关键。不要投入太多。只需勾勒出您想要执行的操作即可:
public class TwentyQuestions{
static void mainQuestioning(){
System.out.println("Is it a living animal, living plant, or non-living thing? ");
String user = get.nextLine();
switch(user){
case "living animal" :
askLivingAnimalQuestions();
break;
case "living plant":
askLivingPlantQuestions();
break;
case "non-living":
askNoneLivingQuestions();
break;
default:
handleWrongInput();
}
}
}
当然,上面的内容无法编译,因为现在尚未实现细节(缺少某些方法)-但是请注意,问题是如何简化很多的(如果没有嵌套的话),并且很可能您可以模仿一下它应该做的。保持简单,直接是关键。
步骤2:现在,您可以轻松地创建到目前为止绘制的方法。让我们这样做:
public class TwentyQuestions{
static void handleWrongInput(){
System.err.println("I am no longer playing with you as you don't answer my question properly");
System.exit(1);
}
static void askLivingAnimalQuestions(){
System.out.println("Does it have feathers, fur, or neither?");
String user = get.nextLine();
switch(user){
case "feathers":
askLivinAnimalWithFeathersQuestions();
break;
case "fur":
askLivinAnimalWithFurQuestions();
break;
default:
handleWrongInput();
}
}
static void askLivingPlantQuestions(){
System.out.println("is it a tree?");
String user = get.nextLine();
if("yes".equals(user)){
System.out.println("So its a tree!");
return;
}
}
static void askNoneLivingQuestions(){
System.out.println("WhateverNoneLivingQuestion ?");
String user = get.nextLine();
switch(user){
//add possible responses here.
default:
handleWrongInput();
}
}
static void mainQuestioning(){
System.out.println("Is it a living animal, living plant, or non-living thing? ");
String user = get.nextLine();
switch(user){
case "living animal" :
askLivingAnimalQuestions();
break;
case "living plant":
askLivingPlantQuestions();
break;
case "non-living":
askNoneLivingQuestions();
break;
default:
handleWrongInput();
}
}
}
现在我进一步解决了这个问题。但是它仍然/不会再次编译,因为带有毛皮的动物和带有羽毛的动物缺少方法。
第3步:也要实施它们:
public class TwentyQuestions{
static void handleWrongInput(){
System.err.println("I am no longer playing with you if you don't answer my question properly");
System.exit(1);
}
static void askLivinAnimalWithFeathersQuestions(){
System.out.println("is it bigger than a soccer ball?");
String user = get.nextLine();
//don't know how you want to continue;
//....
}
static void askLivinAnimalWithFurQuestions(){
System.out.println("is it domesticated?");
String user = get.nextLine();
//don't know how you want to continue;
//....
}
static void askLivingAnimalQuestions(){
System.out.println("Does it have feathers, fur, or neither?");
String user = get.nextLine();
switch(user){
case "feathers":
askLivinAnimalWithFeathersQuestions();
break;
case "fur":
askLivinAnimalWithFurQuestions();
break;
default:
handleWrongInput();
}
}
static void askLivingPlantQuestions(){
System.out.println("is it a tree?");
String user = get.nextLine();
if("yes".equals(user)){
System.out.println("So its a tree!");
return;
}
}
static void askNoneLivingQuestions(){
System.out.println("WhateverNoneLivingQuestion ?");
String user = get.nextLine();
switch(user){
//add possible responses here.
default:
handleWrongInput();
}
}
static void mainQuestioning(){
System.out.println("Is it a living animal, living plant, or non-living thing? ");
String user = get.nextLine();
switch(user){
case "living animal" :
askLivingAnimalQuestions();
break;
case "living plant":
askLivingPlantQuestions();
break;
case "non-living":
askNoneLivingQuestions();
break;
default:
handleWrongInput();
}
}
}
请注意所有导致您遇到麻烦的嵌套if / else如何消失。
完成:现在,如果您另外实施缺少的问题并添加在main(String [] args)中初始化的Scanner“ get”,则应该在那里。现在应该很容易。
好吧..这可能为您提供了许多解决20个嵌套问题的方法:这是由于您拥有的可能性很多。您必须处理许多问题和答案。没办法。 最好让他们干净整洁地放在自己专用的地方,而不是到处乱逛(您整理并把所有东西放在原处–必须处理的案件/问题的数量保持不变)。
但是,在成长的应用程序中,您可以将所有问题和答案放在像树一样的数据结构中。这样一来,您就可以避免使用大量方法,而只需走一些树就可以使用一些通用方法。
[同样,您也可以创建临时方法,该临时方法对所需的东西不执行任何操作(“存根”),但尚未实现以使其在仍在开发时进行编译。 ]
以下是作为完整类的示例,该类会编译并进行查询,直至实现:
import java.util.Scanner;
/**
*
* @author Kai
*/
public class TwentyQuestions {
static Scanner get = new Scanner(System.in);
static void handleWrongInput() {
System.err.println("I am no longer playing with you if you don't answer my question properly");
System.exit(1);
}
static void askLivinAnimalWithFeathersQuestions() {
System.out.println("is it bigger than a soccer ball?");
String user = get.nextLine();
//don't know how you want to continue;
//....
}
static void askLivinAnimalWithFurQuestions() {
System.out.println("is it domesticated?");
String user = get.nextLine();
//don't know how you want to continue;
//....
}
static void askLivingAnimalQuestions() {
System.out.println("Does it have feathers, fur, or neither?");
String user = get.nextLine();
switch (user) {
case "feathers":
askLivinAnimalWithFeathersQuestions();
break;
case "fur":
askLivinAnimalWithFurQuestions();
break;
default:
handleWrongInput();
}
}
static void askLivingPlantQuestions() {
System.out.println("is it a tree?");
String user = get.nextLine();
if ("yes".equals(user)) {
System.out.println("So its a tree!");
return;
}
}
static void askNoneLivingQuestions() {
System.out.println("WhateverNoneLivingQuestion ?");
String user = get.nextLine();
switch (user) {
//add possible responses here.
default:
handleWrongInput();
}
}
static void mainQuestioning() {
System.out.println("Is it a living animal, living plant, or non-living thing? ");
String user = get.nextLine();
switch (user) {
case "living animal":
askLivingAnimalQuestions();
break;
case "living plant":
askLivingPlantQuestions();
break;
case "non-living":
askNoneLivingQuestions();
break;
default:
handleWrongInput();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
mainQuestioning();
}
}
示例运行:
Is it a living animal, living plant, or non-living thing?
living animal
Does it have feathers, fur, or neither?
fur
is it domesticated?
yes
BUILD SUCCESSFUL (total time: 30 seconds)
答案 2 :(得分:0)
您没有正确使用if缩进,在这种情况下,注释也可以为您提供帮助。
System.out.println("Think of Something");
System.out.println("Is it a living animal, living plant, or non-living thing? ");
String user = get.nextLine();
// start method
if (user.equals("living animal")); { //starts animal tree
System.out.println("Does it have feathers, fur, or neither?");
String user2 = get.nextLine();
if (user2.equals("feathers")); {
System.out.println("is it bigger than a soccer ball?");
} else if (user2.equals("fur")); {
System.out.println("is it domesticated?");
}
} else if (user.equals("living plant")); { //starts plant tree
System.out.println("is it a tree?");
} // end method