Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I don't know why, but the error are in the returns.


C#
public class funcoes : MonoBehaviour
{
    int testSumArray(int[] numbers = null, int b = 2)
    {
        numbers = new int[] { 8, 3, 9, 6, 7, 4 };
        int conta = 0;

        for(int i = 0; i< 5; i++)
        {
            if(numbers[i] / b == 0)
            {
                conta = conta + 1;
                return true;
            }
            else
            {
                return false;
            }
        }
        return conta;
        
    }
}


What I have tried:

I tried to search all over the google.
Posted
Updated 26-Apr-22 14:02pm
v2
Comments
Richard Deeming 27-Apr-22 4:32am    
Why are you passing in a numbers array if you're just going to ignore it and overwrite it with your own fixed value?

Why are you using a constant for the loop termination? And why are you ignoring the last element in the array?

Why are you trying to return a bool from a method that should return an int?
Antonio_Rodrigues 3-May-22 11:26am    
because is what my teacher want me to do, and I don't know why.

You can Google this all you want. You'll never find the answer, and you don't need to. You just need to look at your own code.

Where do you define the return type of a method? What does that say?
C#
 |
 |
 v
int testSumArray(int[] numbers = null, int b = 2)
{
You're saying this method will return an integer.

Now look at all the return statements in that method. They they ALL return an integer? No. You've got true and false and then a return conta, which will never execute by the way.

Step through your code, line by line, and think about what that line alone is going to do.

A method will only ever execute any return statement once. Once the code hits a return statement, your method code will no longer run and control will be returned to whatever code called your method.
 
Share this answer
 
Don't know what you intend to do with this code, but here is a possible correction :
C#
public class funcoes : MonoBehaviour
{
    int testSumArray(int[] numbers = null, int b = 2)
    {
        numbers = new int[] { 8, 3, 9, 6, 7, 4 };
        int conta = 0;

        for(int i = 0; i< 5; i++)
        {
            if(numbers[i] / b == 0)
            {
                conta = conta + 1;
                return true;
            }
            >else
            {
                return false;
            }
        }
        return conta;
        
    }
}
 
Share this answer
 
v2
Comments
PIEBALDconsult 26-Apr-22 20:02pm    
Or use break .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900