如何使用&&amp ;?正确检查其他声明?

时间:2014-05-01 17:15:42

标签: java if-statement

只是一个简单的问题,我可以用它来包括“左”“右” 回来“和”向前“一起或者我必须分开做吗?

出现错误,所以如果有人知道如何将它们全部包含在一起那么请帮忙。感谢

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();
    }

3 个答案:

答案 0 :(得分:2)

我会使用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;
    }

答案 1 :(得分:1)

首先回答您的问题,在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陈述之间没有变化空间的重新分配。

答案 2 :(得分:0)

从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();
}