3 回答

TA贡献1829条经验 获得超13个赞
您正在使用以下语法写出您的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基本上,要使orelse if语句正常工作,您所要做的就是删除不需要的分号。

TA贡献1789条经验 获得超10个赞
作为一个例子,说明如何解决一个难以处理的问题。开箱即用的程序并不重要。
当“很难让它合作并控制所有 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();
}
}
}
当然,上面的那个东西不会编译,因为细节现在还没有实现(缺少一些方法) - 但请注意问题是如何简化很多的(没有嵌套的 if),很可能你可以立即看到它应该是什么做。保持简单,直截了当是关键。
第 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) 中初始化的扫描仪“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)

TA贡献2012条经验 获得超12个赞
在这种情况下,您没有正确使用 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 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
添加回答
举报