I'm trying to write an "if" statement with multiple boolean conditions. Right now, I have 5 different boolean variables. In the "if" statement, I want to print different things according to which boolean variable is true. The code I have right now prints out the results from each of the "if" statements, instead of only from the one that is true.
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");
}
Basically, what I want the program to do is only print object "a" when "result" is true. However, right now, when "result" is true, the program prints all the objects (a, b, c, d, and e) instead of just printing "a". How can I fix it?
Aucun commentaire:
Enregistrer un commentaire