在“if”语句中有多个布尔条件?

时间:2017-12-16 10:30:09

标签: if-statement boolean

我正在尝试使用多个布尔条件编写“if”语句。现在,我有5个不同的布尔变量。在“if”语句中,我想根据布尔变量为true来打印不同的东西。我现在拥有的代码打印出每个“if”语句的结果,而不是仅显示真实的结果。

boolean result = Arrays.equals(user, Jamie); //compares the arrays
boolean result2 = Arrays.equals(user, David);
boolean result3 = Arrays.equals(user, Sally);
boolean result4 = Arrays.equals(user, Susan);
boolean result5 = Arrays.equals(user, Mary);

if (result = true) {
        Address a = new Address (20, 201107, "Pizza", "blue"); //makes the different objects
        printLines (a); //calls the printLines method to print out the "Secret information" about the user
} 
if (result2 = true) {
        Address b = new Address (56, 211045, "Noodles", "Pink");
        printLines (b);
}
if (result3 = true) {
        Address c = new Address (46, 329432, "Hamburgers", "Green");
        printLines (c); 
}
if (result4 = true) {
        Address d = new Address (80, 322810, "Fried Rice", "Yellow");
        printLines (d);
}
if (result5 = true) {
        Address e = new Address (35, 405029, "Steak", "Red");
        printLines (e);
}
else {
        System.out.println ("Password is wrong");
}

基本上,我希望程序做的只是当“result”为真时打印对象“a”。但是,现在,当“结果”为真时,程序会打印所有对象(a,b,c,d和e),而不是仅打印“a”。我该如何解决?

2 个答案:

答案 0 :(得分:0)

使用else if确保只评估一个闭包:

if (result) {
    Address a = new Address (20, 201107, "Pizza", "blue"); //makes the different objects
    printLines (a); //calls the printLines method to print out the "Secret information" about the user
} 
else if (result2) {
    Address b = new Address (56, 211045, "Noodles", "Pink");
    printLines (b);
}
else if (result3) {
    Address c = new Address (46, 329432, "Hamburgers", "Green");
    printLines (c); 
}
else if (result4) {
    Address d = new Address (80, 322810, "Fried Rice", "Yellow");
    printLines (d);
}
else if (result5) {
    Address e = new Address (35, 405029, "Steak", "Red");
    printLines (e);
}
else {
    System.out.println ("Password is wrong");
}

如您的代码当前所示,如果多个结果布尔变量为true,则可能会评估多个if块。

答案 1 :(得分:0)

使用

if (result)

而不是

if (result == true)

以下代码说明了......

package com.johanw;

public class StackOverFlow {
    public static void main(String[] args) throws Exception{
        boolean value = false;
        // the below condition is true, i.e. value == false
        if (value == false) {
            System.out.println("Value equals false");
        }
        // the below condition is also true and this is what is the recommended approach for evaluating conditions
        if (!value) {
            System.out.println("Value equals false, i.e. !value is a true condition");
        }
        // the below is another illustration of evaluating a condition, comparing the condition (1 == 1) with true
        if ((1 == 1) == true) {
            System.out.println("The condition 1 == 1 equals true");
        }
        // the below condition is also true and also the recommended approach for evaluating a condition
        if (1 == 1) {
            System.out.println("The condition 1 == 1 equals true");
        }
        // the below line is assigning a value true to the variable value and then evaluting the condition, which is true
        if (value = true) {
            System.out.println("Value was assigned the value true and therefore results in a true condition");
        }
        // the below line is assigning a value false to the variable value and then evaluting the condition, which is false, and hence the else condition is valid
        if (value = false) {
        } else {
            System.out.println("Value was assigned the value false and therefore results in a false condition");
        }
    }
}