简体   繁体   中英

not all code paths return a value ' '

I am trying to make a simple shotgun game where the user vs the CPU and the both pick shot, shield or reload but In my GetOptionFromUser I keep getting the error 'ShotgunGame.Program.GetOptionFromUser()': not all code paths return a value

Any Guidance would be appreciated Here is my methods

enum ShotgunOption
{
    Shoot = 1,
    Reload = 2,
    Shield = 3,
}

static void DisplayMenu()
{
    Console.WriteLine("Please pick an item:");
    Console.WriteLine("S - Shoot");
    Console.WriteLine("P - Shield");
    Console.WriteLine("R - Reload");         
    //Console.WriteLine("X - Exit");
}


static ShotgunOption GetOptionFromUser()
{
    char menuItem;
    DisplayMenu();
    menuItem = char.ToUpper(char.Parse(Console.ReadLine()));

    if (menuItem == 'S')
    {
        return ShotgunOption.Shoot;
    }
    else if (menuItem == 'P')
    {
        return ShotgunOption.Shield;
    }
    else if (menuItem == 'R')
    {
        return ShotgunOption.Reload;
    }

    while (menuItem != 'S' && menuItem != 'P' && menuItem != 'R')
    {
        Console.WriteLine("Error - Invalid menu item");
        DisplayMenu();
        menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
    }
}

static void DisplayResults(ShotgunOption UserOption,ShotgunOption CPUOption, int UserScore, int UserBullets, int CPUBullets)
{
    Console.Clear();
    Console.WriteLine("Giving up?");
    Console.WriteLine("You Chose {0}, The Computer Chose{1} Your Score is {3} . You had {4} Bullet(s). The CPU had {5} bullets(s).", UserOption, CPUOption, UserScore, UserBullets, CPUBullets);
    Console.WriteLine("Thanks for playing!");
    Console.ReadKey();
}

You have a series of if else if 's that return things. But you then have

while (menuItem != 'S' && menuItem != 'P' &&
        menuItem != 'R')
...

with no return statements. You need a return at the end of your method, as well as in your if 's.

What you are trying to do, and what your code does, are a little apart. Something like this will serve you better:

static ShotgunOption GetOptionFromUser()
{
    char menuItem = null;
    DisplayMenu();
    var menuItem = char.ToUpper(char.Parse(Console.ReadLine()));

    while (menuItem != 'S' && menuItem != 'P' && menuItem != 'R')
    {
        Console.WriteLine("Error - Invalid menu item");
        DisplayMenu();
        menuItem = char.ToUpper(char.Parse(Console.ReadLine()));
    }

    if (menuItem == 'S')
    {
        return ShotgunOption.Shoot;
    }
    else if (menuItem == 'P')
    {
        return ShotgunOption.Shield;
    }
    return ShotgunOption.Reload;
}

This way, the code loops until it has a valid value and then handles which item to return after it leaves the loop, ensuring something is returned by every path through the code.

In your GetOptionFromUser function, after the if/else if/else if structure, there are no return statements. Once the program exits that structure and enters the while loop, there is no way for it to return from the function, which is invalid behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM