0

Im new into coding and recently ive been doing some exercise on CodeWars and encounter a problem. Here is a exercise ->1<-

and here is what i wrote:

public static double basicOp(char a, double v1, double v2)
{
  if (a == '+')
    return v1+v2;
  else if (a == '-')
    return v1-v2;
  else if (a == '/')
    return v1/v2;
  else if (a == '*')
    return v1*v2;   
}

Where is a problem? :(( Thanks

muniek7
  • 11
  • 1
  • 3
    What if none of your conditions in the if/else-ifs are true? This is what it's complaining about. This question is coming up a lot today. See: https://stackoverflow.com/questions/21197410/c-sharp-compiler-error-not-all-code-paths-return-a-value – Luke Oct 11 '22 at 16:56
  • 2
    what if a == 'G' ? – Rafael Oct 11 '22 at 16:58
  • Okay i get my mistake but how do i fix it ? without making me rewrite everything... ive looked into these links but i couldnt make any progress... – muniek7 Oct 11 '22 at 17:11
  • Side note, but you might consider using a switch block here – Traveller Oct 11 '22 at 17:12
  • Maybe *you* know that no other character will be used, but the compiler doesn't! – Hans Kesting Oct 11 '22 at 17:12
  • "how do i fix it" - really **think** about: what _if_ `a=='G'`. and when you get the answer what shout happen then: write it in your code. (or: ask your teacher for help.) – Franz Gleichmann Oct 11 '22 at 17:13
  • 1
    If no other character may be passed to this method, just throw an exception (preferably ArgumentOutOfRangeException). If you do want to allow other characters, decide what the method should do if some other character is passed in. – Traveller Oct 11 '22 at 17:14

0 Answers0