HOW TO CALCULATE INSIDE A CLASS? C#

Asked

Viewed 579 times

-5

I’m doing the following program on C#:

Create an abstract class named "Combinatorial Analysis", responsible for specifying, in addition to the factorial calculation of a number, the methods due to Calculation of Permutations, Arrangements and Combinations of elements[...]

I need to make a class called Combinatorial Analysis in which I must perform the factorial calculation of a number that will be typed by the user in the main program.

I know how to make the factorial, but within a class, you’re not even free to do any kind of equal accounts on Static void Main.

That’s the way I’ve done it:

public abstract class AnaliseCombinatoria
    {
        static int Fatorial(int numero)
        {
            for(int i = 0; numero <= 1; i--)
            {
                return numero * (numero - 1);
            }
        }

    }

You are giving an error in the variable "Factorial" saying: "Combinatorial analysis.Factorial(int)": not all code paths return a value

How do you make calculations within a class?

  • 3

    David, your question is vague. Could you show us what you already have and illustrate what you want to do?

  • 1

    Buddy, your mistake is not about being in another class or not. 1 - You said you are not free to do the calculations in a class as you are free in main. But the mainis within a class too, which you probably named after Inicializador or something like. 2 - Your error is simple to solve. static int Fatorial(int numero) {&#xA; &#xA; for (int i = 0; numero <= 1; i--) {&#xA; &#xA; return numero * (numero - 1);&#xA; }&#xA; &#xA; return numero; // É só por esse return aqui que a IDE vai parar de reclamar&#xA; }

  • 1

    Sometimes, when a red or yellow line appears, under your code, you press Ctrl + 1 (If you are in Eclipse) it will give you some options on how to solve a particular problem. In your case, the IDE is complaining that your method is not returning a value in all possible cases, because, if your variable numero for Maior ou igual a 1, will do nothing in FOR and his method would return nothing, and is bound to return something.

  • 2

    First, you are giving a Return inside the loop for or else the first calculation will stop because the Return inside a loop or any runline terminates the command, remove the Return out of the loop.

1 answer

1

Error in question is referring to the return inside the Loop;

Put it like this:

static int Fatorial(int numero)
    {
        for(int i = 0; numero <= 1; i--)
        {
            numero = numero * (numero - 1);
        }
        return numero;
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.