If else statement with 4 outcomes but only 2 seems to work

  • A+
Category:Languages

So I'm working on my first project on programming at my new study. Never tried programming anything before. I want to make it able to choose between 4 different outcomes and a default function but it only seems to be able to choose between the if statement and the second else if. What have I done wrong?

private void userStandby () {                                                                   System.out.println("What do you want to set your membershipstatus to");                            System.out.println("1) Active /n2) Standby");                                                   int newchange = input.nextInt();                                                                ((Medlem) currentUser).setStatus(newchange);                                                   if (((Medlem) currentUser).getStatus() == 1 && newchange == 1) {                                  System.out.println("Your membership is already active");                                                                                                                     } else if (((Medlem) currentUser).getStatus() == 1 && newchange == 2)  {                           System.out.println("Your membership is now on standby");                                   } else if (((Medlem) currentUser).getStatus() == 2 && newchange == 2) {                            System.out.println("Your membership is already on standby");                           } else if (((Medlem) currentUser).getStatus() == 2 && newchange == 1) {                              System.out.println("Your membership is now active");                                       } else                                                                                              System.out.println("Please write 1 or 2");                                       userStandby();                                                                             }                                                                                      

 


((Medlem) currentUser).setStatus(newchange); 

implies that ((Medlem) currentUser).getStatus() == newchange, so of course only two of the branches can be executed - the one where both of them are equal to 1 and the one where both of them are equal to 2.

Perhaps your intention was to call setStatus after the if-else-if-..-else statements.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: