jeudi 2 janvier 2020

why if I enter a negative number in the method, condition else if does not catch it

I have problem with solution in this example.

Input:>6 ///Output:0+1+2+3+4+5+6 = 21 /// Input:> -15 /// Output:-15<0 /// Input:>0 //// Output:0=0

public static string ShowSequence(int n)
{
    int sumInt = 0;
    string sum = "";
    for (int i = 0; i <= n; i++)
    {
        if (n == 0)
        {
            sum += i + "=";
        }
        else if (n < 0)
        {
            sum += n + "<";
        }
        else
       if (i == n)
        {
            sum += i + " = ";
        }
        else
            sum += i + "+";
        sumInt += i;
    }
    sum += sumInt;
    return sum;
}

Everything works except a negative number , because my program return 0 not -15<0. Thanks for help !

Aucun commentaire:

Enregistrer un commentaire