我正在编写一个 Java 程序,该程序旨在根据用户的输入与用户一起播放 20 个问题的版本

为此,我使用了很多嵌套的 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


白衣非少年
浏览 143回答 3
3回答

烙印99

您正在使用以下语法写出您的if语句:if (user2.equals("feathers"));{&nbsp; &nbsp; 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{&nbsp; &nbsp; System.out.println("is it bigger than a soccer ball?"); // <-- This line is ran no matter what the if statement was}if基本上,要使orelse if语句正常工作,您所要做的就是删除不需要的分号。

至尊宝的传说

作为一个例子,说明如何解决一个难以处理的问题。开箱即用的程序并不重要。当“很难让它合作并控制所有 if/else 语句”时,它回答了如何简化事情的问题。如果您愿意,可以为这种情况提供一种策略。我也做了一些演示。在实践中,你会做看起来很方便的事情。另外:为了简单起见,我将所有内容都设置为静态 - 在成长的应用程序中,您肯定会使用实例。第 1 步:您从一个非常简单的类框架开始。简单是关键。不要投入太多。只需画出你知道你想要它做什么:public class TwentyQuestions{&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;static void mainQuestioning(){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Is it a living animal, living plant, or non-living thing? ");&nbsp; &nbsp; &nbsp; &nbsp; String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; switch(user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living animal" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingAnimalQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living plant":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingPlantQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "non-living":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askNoneLivingQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;}当然,上面的那个东西不会编译,因为细节现在还没有实现(缺少一些方法) - 但请注意问题是如何简化很多的(没有嵌套的 if),很可能你可以立即看到它应该是什么做。保持简单,直截了当是关键。第 2 步:现在您可以轻松地创建到目前为止所绘制的方法。让我们这样做:public class TwentyQuestions{&nbsp; &nbsp;static void handleWrongInput(){&nbsp; &nbsp; &nbsp; System.err.println("I am no longer playing with you as you don't answer my question properly");&nbsp; &nbsp; &nbsp; System.exit(1);&nbsp; &nbsp;}&nbsp; &nbsp;static void askLivingAnimalQuestions(){&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Does it have feathers, fur, or neither?");&nbsp; &nbsp; &nbsp; &nbsp;String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp;switch(user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "feathers":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivinAnimalWithFeathersQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case&nbsp; "fur":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;askLivinAnimalWithFurQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;static void askLivingPlantQuestions(){&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("is it a tree?");&nbsp; &nbsp; &nbsp; &nbsp;String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp;if("yes".equals(user)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("So its a tree!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;static void&nbsp; askNoneLivingQuestions(){&nbsp; &nbsp; &nbsp;System.out.println("WhateverNoneLivingQuestion ?");&nbsp; &nbsp; &nbsp; &nbsp;String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp;switch(user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//add possible responses here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;static void mainQuestioning(){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Is it a living animal, living plant, or non-living thing? ");&nbsp; &nbsp; &nbsp; &nbsp; String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; switch(user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living animal" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingAnimalQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living plant":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingPlantQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "non-living":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askNoneLivingQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;}现在我把问题分解得更厉害了。但它仍然/再次无法编译,因为缺少用于有毛皮的动物和有羽毛的动物的方法。第 3 步:也实施它们:public class TwentyQuestions{&nbsp; &nbsp;static void handleWrongInput(){&nbsp; &nbsp; &nbsp; System.err.println("I am no longer playing with you if you don't answer my question properly");&nbsp; &nbsp; &nbsp; System.exit(1);&nbsp; &nbsp;}&nbsp; &nbsp;static void&nbsp; askLivinAnimalWithFeathersQuestions(){&nbsp; &nbsp; &nbsp; System.out.println("is it bigger than a soccer ball?");&nbsp; &nbsp; &nbsp; String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; //don't know how you want to continue;&nbsp; &nbsp; &nbsp; //....&nbsp; &nbsp;}&nbsp; &nbsp;static void askLivinAnimalWithFurQuestions(){&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("is it domesticated?");&nbsp; &nbsp; &nbsp; &nbsp;String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; //don't know how you want to continue;&nbsp; &nbsp; &nbsp; //....&nbsp;&nbsp; &nbsp;}&nbsp;&nbsp; &nbsp;static void askLivingAnimalQuestions(){&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Does it have feathers, fur, or neither?");&nbsp; &nbsp; &nbsp; &nbsp;String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp;switch(user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "feathers":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivinAnimalWithFeathersQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case&nbsp; "fur":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;askLivinAnimalWithFurQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;static void askLivingPlantQuestions(){&nbsp; &nbsp; &nbsp; &nbsp;System.out.println("is it a tree?");&nbsp; &nbsp; &nbsp; &nbsp;String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp;if("yes".equals(user)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("So its a tree!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;static void&nbsp; askNoneLivingQuestions(){&nbsp; &nbsp; &nbsp;System.out.println("WhateverNoneLivingQuestion ?");&nbsp; &nbsp; &nbsp; &nbsp;String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp;switch(user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//add possible responses here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp;}&nbsp; &nbsp;static void mainQuestioning(){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Is it a living animal, living plant, or non-living thing? ");&nbsp; &nbsp; &nbsp; &nbsp; String&nbsp; user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; switch(user){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living animal" :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingAnimalQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living plant":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingPlantQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "non-living":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askNoneLivingQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp;&nbsp;}请注意所有导致您麻烦的嵌套 if/else 是如何消失的。完成:现在,如果您另外实现缺少的问题并添加一个在 main(String[] args) 中初始化的扫描仪“get”,那么您应该在那里。现在应该很容易了。嗯.. 这可能为您提供了很多方法来解决 20 个嵌套问题:这是由于您拥有的可能性的数量。你必须处理那么多的问题和答案。没办法绕过去。最好让他们干净地呆在自己专用的地方,而不是在某个地方闲逛(你整理好并将所有东西放在它的位置 - 你必须处理的案件/问题的数量保持不变)。然而,在一个成熟的应用程序中,您可以将所有问题和答案放在一个像树一样的数据结构中。这样你就可以避免大量的方法,并有一些通用的方法,而不是只走树....[您也可以为您需要但尚未实现的东西创建什么都不做的临时方法(“存根”),以便在您仍在开发时使其编译。]这是一个完整类的示例,它编译并执行到目前为止的问题:import java.util.Scanner;/**&nbsp;*&nbsp;* @author Kai&nbsp;*/public class TwentyQuestions {&nbsp; &nbsp; static Scanner get = new Scanner(System.in);&nbsp; &nbsp; static void handleWrongInput() {&nbsp; &nbsp; &nbsp; &nbsp; System.err.println("I am no longer playing with you if you don't answer my question properly");&nbsp; &nbsp; &nbsp; &nbsp; System.exit(1);&nbsp; &nbsp; }&nbsp; &nbsp; static void askLivinAnimalWithFeathersQuestions() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("is it bigger than a soccer ball?");&nbsp; &nbsp; &nbsp; &nbsp; String user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; //don't know how you want to continue;&nbsp; &nbsp; &nbsp; &nbsp; //....&nbsp; &nbsp; }&nbsp; &nbsp; static void askLivinAnimalWithFurQuestions() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("is it domesticated?");&nbsp; &nbsp; &nbsp; &nbsp; String user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; //don't know how you want to continue;&nbsp; &nbsp; &nbsp; &nbsp; //....&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; static void askLivingAnimalQuestions() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Does it have feathers, fur, or neither?");&nbsp; &nbsp; &nbsp; &nbsp; String user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; switch (user) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "feathers":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivinAnimalWithFeathersQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "fur":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivinAnimalWithFurQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static void askLivingPlantQuestions() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("is it a tree?");&nbsp; &nbsp; &nbsp; &nbsp; String user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; if ("yes".equals(user)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("So its a tree!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static void askNoneLivingQuestions() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("WhateverNoneLivingQuestion ?");&nbsp; &nbsp; &nbsp; &nbsp; String user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; switch (user) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add possible responses here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; static void mainQuestioning() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Is it a living animal, living plant, or non-living thing? ");&nbsp; &nbsp; &nbsp; &nbsp; String user = get.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; switch (user) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living animal":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingAnimalQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "living plant":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askLivingPlantQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "non-living":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; askNoneLivingQuestions();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleWrongInput();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* @param args the command line arguments&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; mainQuestioning();&nbsp; &nbsp; }}示例运行:Is it a living animal, living plant, or non-living thing?&nbsp;living animalDoes it have feathers, fur, or neither?furis it domesticated?yesBUILD SUCCESSFUL (total time: 30 seconds)

繁花如伊

在这种情况下,您没有正确使用 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();// start methodif (user.equals("living animal")); { //starts animal tree&nbsp; System.out.println("Does it have feathers, fur, or neither?");&nbsp; String user2 = get.nextLine();&nbsp; if (user2.equals("feathers")); {&nbsp; &nbsp; System.out.println("is it bigger than a soccer ball?");&nbsp; } else if (user2.equals("fur")); {&nbsp; &nbsp; System.out.println("is it domesticated?");&nbsp; }} else if (user.equals("living plant")); { //starts plant tree&nbsp; System.out.println("is it a tree?");} // end method
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java