如何使用 && 正确检查 else 语句?



只是一个简单的问题,我可以用它来包含"左"右"后退"和"前进"一起做,还是我必须分开做?

出现了一个错误,所以如果有人知道如何将它们全部包含在一起,请提供帮助。 谢谢

Scanner console = new Scanner(System.in);
for (int i = 0; i < 100; i++) {
    System.out.println("Please type in either the word "left" or "right" or "back" or "foward": ");
    String s = console.next();
    if (s.equalsIgnoreCase("left")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(0,100,S);
    } if (s.equalsIgnoreCase("right")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(100,0,S);
    } if (s.equalsIgnoreCase("back")) {
        myFinch.setWheelVelocities(-100,-100,S);
    } if (s.equalsIgnoreCase("foward")) {
        myFinch.setWheelVelocities(130,130,S);
    } else if (s.equalsIgnoreCase != ("left" && "right" && "back" && "foward")) {
        myFinch.quit();
    }

我会使用一个switch语句:

switch (s.toLowerCase()) {
    case "left":
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(0,100,S);
        break;
    case "right":
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(100,0,S);
        break;
    }

首先回答你的问题,在Java你应该使用String.equals来比较字符串,或者String.equalsIgnoreCase。 这是因为此示例将失败:

String a = "a";
if (a == "a") {
    // Will not be true because you are comparing the reference to the string "a" 
} else if (a.equals("a")) {
    // Will work because you are comparing on the value of the two strings
}

参考: == vs .equals

我注意到你在前几个陈述中这样做了,但在最后一个陈述中,即有问题的陈述中,你没有这样做。

虽然您尝试形成的声明是不必要的,但我发现分享正确的方法会很有用:

    // OMITTED CODE
    } else if (s.equalsIgnoreCase("left") && s.equalsIgnoreCase("right") && s.equalsIgnoreCase("back") && s.equalsIgnoreCase("foward") ) {
        myFinch.quit();
    }

您必须使每个boolean语句完整,从某种意义上说,它必须评估为boolean

s.equalsIgnoreCase != x// this is simply  method so it could not be compared to anything using the != operator
("left" && "right" /* etc */ ) // "left", "right" are not booleans but simply strings.

Java是一种非常明确的语言,因此您尝试的快捷方式通常相距甚远。

其次,您应该使用以下格式:

if (/* condition 1*/) {
    // code if condition 1 is true
} else if (/* condtion 2 */) {
    // code if condition 2 is true but condition 1 is false
} else {
    // code if condition 1 and condition 2 are false
}

else if 语句用于简化采用以下格式的代码:

if (/* condition */) {
    // code will run if condtion is true
} else {
    if (/* sub-condition */) {
        // code will run if sub-condition is true, but condition is false
    } else {
        if (/* sub-sub-condition */) {
            // code will run if sub-sub-condition is true, but sub-condition and condition are false
        } else {
            // code will run if condition, sub-sub-condition, and sub-condition is false
        }
    }
}

要避免此类代码的长链,请执行以下操作:

if (/* condition */) {
    // code will run if condtion is true
} else { if (/* sub-condition */) {
    // code will run if sub-condition is true, but condition is false
} else { if (/* sub-sub-condition */) {
    // code will run if sub-sub-condition is true, but sub-condition and condition are false
} else {
    // code will run if condition, sub-sub-condition, and sub-condition is false
}}}

从这里到当前设置可以清楚地看到格式:

if (/* condition */) {
    // code will run if condtion is true
} else if (/* sub-condition */) {
    // code will run if sub-condition is true, but condition is false
} else if (/* sub-sub-condition */) {
    // code will run if sub-sub-condition is true, but sub-condition and condition are false
} else {
    // code will run if condition, sub-sub-condition, and sub-condition is false
}

创建这些语句是为了以逻辑方式读取:

If the first condtion is met follow the first set of instructions,
else if the first condition wasnt met then try the second condition and instructions,
else if the first two conditions failed try the third set!,
else Damn! Just resort to these instructions

想象一下你正在照顾朋友的猫的场景。 在您的朋友离开之前,您无法谈论如何照顾猫,但他们给您留下了一组说明:

Dear friend,
    Thank you for looking after muffins.  She is a very high maintenance cat.
 She has four kinds of food and depending on her mood you should feed her one of
 these four:  "Purina Super Awesome Cat Time", "Cat Feast 2000", "Cat Chow", and 
 "Canned".
 If you come over and she is waiting at the door give her the "Cat Fest 2000",
 If she is not waiting at the door, but instead attacks your leg as you enter the
 house you should give her the "Cat Chow",
 If she is not at the door, and didn't attack you but is instead wearing a small hat
 you should give her the "Purina Super Awesome Cat Time" and play a game of Bridge with
 her.
 If none of those things happened then give her the "Canned".
 Thanks!  See you Caturday!

与其让自己完成这项艰巨的任务,明确概述危险,也许我们想写一个非常聪明的机器人,每天进去照顾猫。

 //  Upon arrival
 if ( Cat.isWaitingAtTheDoor() ) {
     Cat.feed("Cat Fest 2000");
 } else if ( Cat.didAttackWhenYouWalkedIn() ) {
     Cat.feed("Cat Chow");
 } else if ( Cat.isWearingSmallHat() ) {
     Cat.feed("Purina Super Awesome Cat Time");
     Cat.playBridgeWith(self);
 } else {
     Cat.feed("Canned");
 }

因此,重新格式化您的代码以匹配该结构,您会发现您不需要最后一个条件:

Scanner console = new Scanner(System.in);
for (int i = 0; i < 100; i++) {
    System.out.println("Please type in either the word "left" or "right" or "back" or "foward": ");
    String s = console.next();
    if (s.equalsIgnoreCase("left")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(0,100,S);
    } else if (s.equalsIgnoreCase("right")) {
        myFinch.setWheelVelocities(90,90,S);
        myFinch.setWheelVelocities(100,0,S);
    } else if (s.equalsIgnoreCase("back")) {
        myFinch.setWheelVelocities(-100,-100,S);
    } else if (s.equalsIgnoreCase("foward")) {
        myFinch.setWheelVelocities(130,130,S);
    } else {
        myFinch.quit();
    }
}

您最初设置它的方式基本上不是在创建分支结构。

考虑一下:

int i = 0;
if (i == 0) {
    System.out.println("i = 0");
    i = 1;
} if (i == 1) {
    System.out.println("i = 1");
} else {
    System.out.println("i is neither 1 or 0");
}

这将输出:

i = 0
i = 1

不是我们想要的!

这是因为上面的代码等效于:

int i = 0;
if (i == 0) {
    System.out.println("i = 0");
    i = 1;
}
// Two separate statements altogether

if (i == 1) {
    System.out.println("i = 1");
} else {
    System.out.println("i is neither 1 or 0");
}

而:

int i = 0;
if (i == 0) {
    System.out.println("i = 0");
    i = 1;
} else if (i == 1) {
    System.out.println("i = 1");
} else {
    System.out.println("i is neither 1 or 0");
}

将给予:

i = 0

我们想要的,现在它是一个分支语句,它检查第一个if语句,然后检查后面的所有else if语句,最后如果没有true则诉诸else语句。 这似乎是您的意图,因为这些if语句之间没有变量重新分配的空间。

从 JDK 7 开始,您可以在交换机中使用字符串。

方法:

switch(s.toLowerCase()) {
case "left":
    myFinch.setWheelVelocities(90,90,S);
    myFinch.setWheelVelocities(0,100,S);
break;
case "right":
    myFinch.setWheelVelocities(90,90,S);
    myFinch.setWheelVelocities(100,0,S);
break;
case 'back':
    myFinch.setWheelVelocities(-100,-100,S);
break;
case "foward"
    myFinch.setWheelVelocities(130,130,S);
break;
/** .. and other cases **/
default:
    myFinch.quit();
}

最新更新