[英]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 我正在尝试制作一个简单的shot弹枪游戏,在该游戏中,用户与CPU以及两者都可以射击,屏蔽或重新加载,但是在我的GetOptionFromUser中,我不断收到错误消息“ ShotgunGame.Program.GetOptionFromUser()”:并非所有代码路径都返回值
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. 您有一系列的if
else if
返回结果。 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. 您需要在方法末尾以及if
的末尾返回。
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. 在您的GetOptionFromUser
函数中,在if/else if/else if
结构之后,没有return语句。 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. 程序退出该结构并进入while
循环后,就无法从函数返回,这是无效行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.