Why am I getting this error: not all code paths return a value?

前端 未结 5 1606
长发绾君心
长发绾君心 2021-01-16 21:15

hi im new to c# and was trying to code but getting error can anybody help me with this what am i doing wrong?

using System;
using System.Collections.Generic         


        
5条回答
  •  难免孤独
    2021-01-16 21:55

    @jalf: is correct about the case where 2 <= n/2 (i.e., 4<=n). You will never enter the for loop in that case, so you need the return after the for.

    As @Kenny suggested,

    if ((n % 1) == 0)
    

    Is suspect. n % 1 always == n, so the condition will only be true when n == 0. However, it also looks like this might be a typo, since the condition does not test anything that varies within the loop. Did you mean

    if ((n % i) == 0)
    

    ?

提交回复
热议问题