我创建了一个玩家类,并为我的菜单驱动的玩家系统制作了一个数组,我试图询问用户他/她是否想要更新目标,如果他们输入y让他们输入新的金额和如果没有显示消息,然后让他们更新助手并为助手做同样的事情,但是当我把n放入程序时,它一直说Exception是unhanded一个未处理的异常类型' System.Exception'发生在SystemPlayer.exe中。我不知道如何解决这个问题
任何帮助将不胜感激
static void ProcessUpdate(Int32 number, String firstName, String lastName,
Int32 goals, Int32 assists, Player[] players,
ref Int32 playerCount, Int32 MAXPLAYERS)
{
int playerindex;//index of the player number in Array
char answer, answer2;
if (playerCount == 0)
{
Console.WriteLine("\nUpdate Player: roster is empty");
}
else if (playerCount < MAXPLAYERS)
{
number = IOConsole.GetInt32("\nUpdate Player: please enter the player's number: ");
//number = IOConsole.GetInt32(message);
//Console.ReadLine();
playerindex = GetPlayerIndex(number, firstName, lastName, goals, assists, players, ref playerCount);
if (playerindex != -1)
{
Console.WriteLine("\nUpdate Player: Player {0} currently has {1} goals and {2} assists \nEdit Goals?: Y/N: ", players[playerindex].Number,
players[playerindex].Goals, players[playerindex].Assists);
answer = Convert.ToChar(Console.ReadLine());
if (answer.Equals('Y') || answer.Equals('y'))
{
goals = IOConsole.GetInt32("\nUpdate Player: please enter the player's new Goal total: ");
if (goals < 0)
{
Console.WriteLine("Goals cannot have a negative number");
}
else if (answer.Equals('N') || answer.Equals('n'))
{
Console.WriteLine("Goals Not Updated");
}
}
answer2 = IOConsole.GetChar("\nEdit Assists?: Y/N: ");
if (answer2.Equals('Y') || answer2.Equals('y'))
{
assists = IOConsole.GetInt32("\nUpdate Player: please enter the player's new Assists total: ");
if (assists < 0)
{
Console.WriteLine("assists Cannot have assists negative number");
}
if (answer2.Equals('N') || answer2.Equals('n'))
{
Console.WriteLine("Assists Not Updated");
}
}
//players[playerindex].LastName = lastName;
//players[playerindex].FirstName = firstName;
players[playerindex].Goals = goals;
players[playerindex].Assists = assists;
Console.WriteLine("\n{0,7} {1,-20}{2, -20}{3,8}{4,8}{5,8}\n", "Number", "First Name", "Last Name", "Goals", " Assists", "Points");
Console.WriteLine("{0,7} {1,-20}{2, -20}{3,8}{4,8}{5,8}",
players[playerindex].Number, players[playerindex].FirstName, players[playerindex].LastName,
players[playerindex].Goals, players[playerindex].Assists, players[playerindex].Points());
Console.WriteLine("Sucessfully Updated!");
Console.WriteLine();
}
else
Console.WriteLine("\nUpdate Player: the player number does not exists");
}
else
Console.WriteLine("\nUpdate Player: the player does not exist in the roster");
}
这是我的播放器类发生异常错误的地方
//Public Goals accessor
public Int32 Goals
{
get
{
//Return member variable value
return _goals;
}
set
{
//Validate value and throw exception if necessary
if (value <= 0)
throw new Exception("Goals must be a positive number");
else
//Otherwise set member variable value
_goals = value;
}
}
答案 0 :(得分:0)
提供的代码几乎没有问题,如果您对任何问题说No
,处理它的if
是 INSIDE Yes
声明< / p>
该属性句柄<= 0
的setter,但您的代码只检查< 0
你应该有一个保存新值的变量,如果有效则将其分配给实际变量,否则如果用户输入的值小于1则会崩溃
执行此操作,再次测试是否确实解决了问题
奖励提示,请检查此String.Equals Method (String, StringComparison)以取消对Y / y和N / n的检查