2017-12-16 11 views
0

여러 개의 부울 조건이있는 "if"문을 작성하려고합니다. 지금은 5 가지 부울 변수가 있습니다. "if"문에서 부울 변수가 참인 경우에 따라 다른 것을 인쇄하려고합니다. 필자가 지금 가지고있는 코드는 각각의 "if"문에서 나온 결과를 인쇄합니다."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"); 
} 

기본적으로 "결과"가 true 일 때만 프로그램에서 "a"를 인쇄합니다. 그러나 "결과"가 참일 때 프로그램은 "a"를 인쇄하는 대신 모든 개체 (a, b, c, d 및 e)를 인쇄합니다. 어떻게 해결할 수 있습니까?

+0

결과 = TRUE ...

if (result == true) 

아래의 코드를 나타낸다. 그것은 할당처럼 보입니다, 조건을 검사하기 위해 ==를 사용하십시오/ – Venkat

답변

0

사용 else if은 하나의 폐쇄 적으로 평가 될 수 있도록하기 위해 : 여러 결과 부울 변수 true 인 경우 코드가 현재 서

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

으로, 다음 대해 여러 개의 if 블록

평가 될 수 있습니다.

+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. 답변을 업데이트했습니다. –