2017-12-16 4 views
0

私は複数のブール条件で "if"文を書くことを試みています。今、5つの異なるブール変数があります。 "if"ステートメントでは、どのブール変数が真であるかによって異なるものを出力したいと思います。私が今持っているコードは、それぞれの "if"ステートメントからの結果を、真であるものからではなく、実際に出力します。"if"文に複数のブール値condtionsを持つか?

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

基本的には、「結果」が真であるときに印刷オブジェクト「a」を印刷するだけです。ただし、 "result"がtrueの場合は、 "a"を印刷するのではなく、すべてのオブジェクト(a、b、c、d、e)を印刷します。どうすれば修正できますか?

+0

結果=真...

if (result == true) 

以下のコードが示すの。それは割り当てのように思えますが、条件をチェックするために==を使用します/ – Venkat

答えて

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

を複数の結果boolean型の変数が、その後mulitple ifブロックが評価される可能性がありますtrueた場合、現在、立っています。

+0

私はこれを行うと、最初の "if"文だけを出力します。 –

+0

@celinatalaはい、それはあなたが望む動作ではありませんか? –

+0

'='は代入のため、最初の文は常に真です。比較のために '=='が必要です。または、 'result == true'が冗長であるため' if(result) 'だけです。 – JJJ

0

使用

if (result) 

代わり

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"); 
     } 
    } 
} 
+0

この回答に追加する必要があります。それは現在のところ単なるコメントです。 –

+0

@TimBiegeleisenに感謝しています。私は答えを更新しました –

関連する問題