ERROR: Not all code paths return a vale - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ERROR: Not all code paths return a vale

visper007visper007 Posts: 2Member
the error is CalculateScore, BTW I'm a newbie in C#. this is a method from a game I'm working on. MasterMind.


public static long CalculateScore(byte chances, long millisec)
{
int score;
byte num1 = chances++;
byte num2 = (byte)10;
byte num3 = (byte)10;
switch (Program.currLevelIndex)
{
case (byte)7:
num2 = (byte)10;
break;
case (byte)11:
num2 = (byte)20;
break;
case (byte)15:
num2 = (byte)40;
break;
}

if ((int)chances >= 1 && (int)chances <= 3)
num1 = (byte)40;
else if ((int)chances >= 4 && (int)chances <= 6)
num1 = (byte)20;
else if ((int)chances > 7)
num1 = (byte)10;
if (millisec > 480000L)
num3 = (byte)10;
else if (millisec >= 240000L && millisec <= 480000L)
num3 = (byte)20;
else if (millisec < 240000L)
num3 = (byte)40;
try
{
score = Convert.ToInt32((int)num2 * (int)num1 * (int)num3);
}
catch (Exception ex)
{
score = 0;
}

Console.Clear();

Console.SetCursorPosition(Program.x, Program.y);
Console.Write("Your Score was: " + score);
}

Comments

  • mohitkumarmohitkumar indiaPosts: 14Member

    Here i am giving the solution i think it will work.The problem is method having return type so you have to write the return statement `public static long CalculateScore(byte chances, long millisec) { int score; byte num1 = chances++; byte num2 = (byte)10; byte num3 = (byte)10; switch (Program.currLevelIndex) { case (byte)7: num2 = (byte)10; break; case (byte)11: num2 = (byte)20; break; case (byte)15: num2 = (byte)40; break; }

            if ((int)chances >= 1 && (int)chances <= 3)
                num1 = (byte)40;
            else if ((int)chances >= 4 && (int)chances <= 6)
                num1 = (byte)20;
            else if ((int)chances > 7)
                num1 = (byte)10;
            if (millisec > 480000L)
                num3 = (byte)10;
            else if (millisec >= 240000L && millisec <= 480000L)
                num3 = (byte)20;
            else if (millisec < 240000L)
                num3 = (byte)40;
            try
            {
                score = Convert.ToInt32((int)num2 * (int)num1 * (int)num3);
            }
            catch (Exception ex)
            {
                score = 0;
            }
    
            Console.Clear();
    
            Console.SetCursorPosition(Program.x, Program.y);
            Console.Write("Your Score was: " + score);
            return 0;
        } `
    

    For similar kind of c# interview questions you can refer this link

Sign In or Register to comment.