vendredi 26 juin 2020

C# - Stuck on getting information from if

I've got this homework from school that I have to do this : Create a program that reads 10 numbers from the interval <-100; 100> on the input. Verify the input values. After entering, determine how many numbers are positive and calculate the diameter only from these numbers.

And I am stuck on the part after determining how many numbers are positive. Because I can't get the information from if outside so I can count them all for a diameter.

So I would be really glad if someone could help me how to move on. Or even how to simplify my code because it is probably too long when it could be like a few lines. Code:

Random number = new Random();
        int a = number.Next(-100, 100);
        int b = number.Next(-100, 100);
        int c = number.Next(-100, 100);
        int d = number.Next(-100, 100);
        int e = number.Next(-100, 100);
        int f = number.Next(-100, 100);
        int g = number.Next(-100, 100);
        int h = number.Next(-100, 100);
        int i = number.Next(-100, 100);
        int j = number.Next(-100, 100);
        Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}", a, b, c, d, e, f, g, h, i, j);



        if (a > 0)
        {
            Console.WriteLine(a + " Is a positive number");
            return a;
        }  
            if (b > 0)

                Console.WriteLine(b + " Is a positive number");
            if (c > 0)

                Console.WriteLine(c + " Is a positive number");
            if (d > 0)

                Console.WriteLine(d + " Is a positive number");
            if (e > 0)
                Console.WriteLine(e + " Is a positive number");
            if (f > 0)
                Console.WriteLine(f + " Is a positive number");
            if (g > 0)
                Console.WriteLine(g + " Is a positive number");
            if (h > 0)
                Console.WriteLine(h + " Is a positive number");
            if (i > 0)
                Console.WriteLine(i + " Is a positive number");
            if (j > 0)
                Console.WriteLine(j + " Is a positive number");

I can't recall the variables or get them out of if.




Aucun commentaire:

Enregistrer un commentaire