samedi 16 juillet 2016

Questions on if and else statements

This is the question I saw on CodingBat:

Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.

And this is the code I have written:

public int max1020(int a, int b) {
  if ( a>=10 && a<=20 && b>=10 && b<=20 && a>b)
  return a;
  if (a>=10 && a<=20 && b>=10 && b<=20 && b>a)
  return b;
  if (a>=10 && a<=20 && b<=10 || b>=20)
  return a;
  if (a<=10 || a>=20 && b>=10 && b<=20)
  return b;

  else return 0;
}

I am fairly confident that it is correct but still then I click run, the websites says that: max1020(9, 21) → 0 BUT my code returns 9. Can someone help me to check through my codes what is wrong with it? :)

Aucun commentaire:

Enregistrer un commentaire