更新值时询问用户y / n [英] Asking user y/n when updating values

查看:58
本文介绍了更新值时询问用户y / n的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个玩家类,并为我的菜单驱动的玩家系统制作了一个数组,我试图询问用户他/她是否想要更新目标,如果他们输入y让他们输入新的金额和如果没有显示消息,然后让他们更新助攻并为助攻做同样的事情但是当我把n放入程序时它会将目标设置为0而不是保留原始值。我不确定如何解决这个问题



任何帮助将不胜感激



I created a player class and made a array from that class for my menu driven player system I am trying to ask the user y/n if they want to update the goals and if they input y have them input the new amount and if no display a message and then ask them to update the assists and do the same for assists but when I put n into the program it keeps setting goals as 0 instead of leaving as it's original value. I am not sure on how to fix this

any help would be appreciated

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
          Int32 newgoals =0, newassits =0;
          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 ", players[playerindex].Number,
                           players[playerindex].Goals, players[playerindex].Assists);
                      // answer = IOConsole.GetChar(Console.ReadLine());

                  answer = IOConsole.GetChar("\n Edit Goals?: Y/N: ");


                  if (answer.Equals('Y') || answer.Equals('y'))
                  {
                      newgoals = IOConsole.GetInt32("\nUpdate Player: please enter the player's new Goal total: ");
                      players[playerindex].Goals = newgoals;
                  }
                    if(goals < 0)
                    {
                        Console.WriteLine("Goals cannot have a negative number");
                    }
                    else if (answer.Equals('N') || answer.Equals('n'))
                       {
                           Console.WriteLine("Goals Not Updated");
                           players[playerindex].Goals = goals;


                       }




                      answer2 = IOConsole.GetChar("\nEdit Assists?: Y/N: ");


                      if (answer2.Equals('Y') || answer2.Equals('y'))
                      {

                          newassits = IOConsole.GetInt32("\nUpdate Player: please enter the player's new Assists total: ");
                          players[playerindex].Assists = newassits;
                      }
                          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].Assists = assists;

                           }



                           //players[playerindex].LastName = lastName;
                           //players[playerindex].FirstName = firstName;
                           //players[playerindex].Goals = newgoals;
                          // players[playerindex].Assists = newassits;
                           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");
           }

推荐答案

if (answer.Equals('Y') || answer.Equals('y'))
{
    newgoals = IOConsole.GetInt32("\nUpdate Player: please enter the player's new Goal total: ");
    players[playerindex].Goals = newgoals;
}
if(goals < 0)
{
    Console.WriteLine("Goals cannot have a negative number");
}
else if (answer.Equals('N') || answer.Equals('n'))
{
    Console.WriteLine("Goals Not Updated");
    players[playerindex].Goals = goals;
}



如果您正确格式化代码,就像我在这里所做的那样,您会看到 else 子句连接到错误的 if 。在发布问题之前,请先努力进行基本的桌面检查和调试。


Had you formatted your code properly, as I have done here, you would see that your else clause is connected to the wrong if. Please make an effort to do basic desk checks and debugging before posting your question.


这篇关于更新值时询问用户y / n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆