dimanche 15 mars 2020

Is there a way to refactor these if statements?

just wondering if there's a way to refactor the below code? I'm new to Java and trying to have DRY code - the below I've written but seems like a lot of conditionals to check

void printDirection() {
  if (yDirection > 0) {
    if (xDirection < 0) {
      println("Travelling South-West");
    } else {
      println("Travelling South-East");
    }
  } else if (yDirection < 0) {
    if (xDirection <0) {
      println("Travelling North-West");
    } else {
      println("Travelling North-East");
    }
  }
}

Thanks in advance for any help!

Aucun commentaire:

Enregistrer un commentaire