2013-10-19 16 views
0

所以我以前問過這個問題,但似乎無法得到任何地方,所以我想我會再問,也許有人可以幫助我現在已經得到另一部分我的代碼工作。所以我需要知道我如何更新和刪除條目。我想澄清一點,我想通過用戶輸入來實現這一點。我如何更新和修改我的陣列與userinput

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace PlayerSystem6 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      //The MAXPLAYERS constant is the physical table size 
      const Int32 MAXPLAYERS = 23; 

      //Declare the player tables 
      Int32[] playerNumbers = new Int32[MAXPLAYERS]; 
      String[] playerLastNames = new String[MAXPLAYERS]; 
      Int32[] playerPoints = new Int32[MAXPLAYERS]; 

      //Keep track of the actual number of players (i.e. logical table size) 
      Int32 playerCount = 0; 

      //Main Driver 
      char menuItem; 
      Console.WriteLine("Welcome to the player system...\n"); 
      menuItem = GetMenuItem(); 
      while (menuItem != 'X') 
      { 
       ProcessMenuItem(menuItem, playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS); 
       menuItem = GetMenuItem(); 
      } 
      Console.WriteLine("\nThank you, goodbye"); 
      Console.ReadLine(); 
     } 

     //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller 
     static char GetMenuItem() 
     { 
      char menuItem; 
      DisplayMenu(); 
      menuItem = char.ToUpper(char.Parse(Console.ReadLine())); 
      while (menuItem != 'C' && menuItem != 'R' //JG 
       && menuItem != 'L' && menuItem != 'X' && menuItem != 'U') 
      { 
       Console.WriteLine("\nError - Invalid menu item"); 
       DisplayMenu(); 
       menuItem = char.ToUpper(char.Parse(Console.ReadLine())); 
      } 
      return menuItem; 
     } 

     static void DisplayMenu() 
     { 
      Console.WriteLine("\nPlease pick an item:"); 
      Console.WriteLine("C - Create Player"); 
      Console.WriteLine("R - Retrieve Player"); 
      Console.WriteLine("U - Update Player"); 
      Console.WriteLine("D - Delete Player"); 
      Console.WriteLine("L - List Players"); 
      Console.WriteLine("X - Exit"); 
     } 

     //Routes to the appropriate process routine based on the user menu choice 
     static void ProcessMenuItem(Char menuItem, Int32[] playerNumbers, String[] playerLastNames, 
      Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS) 
     { 
      switch (menuItem) 
      { 
       case 'C': 
        ProcessCreate(playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS); 
        break; 
       case 'L': 
        ProcessList(playerNumbers, playerLastNames, playerPoints, playerCount); 
        break; 
       case 'R': //JG 
        ProcessRetrieve(playerNumbers, playerLastNames, playerPoints, ref playerCount, MAXPLAYERS); 
        break; 
       case 'U': 
        ProcessUpdate(playerNumbers, playerLastNames, playerPoints, playerCount, MAXPLAYERS); 
        break; 

      } 
     } 

     //Creates a player in the tables if the array is not already full and the name is not a duplicate 
     static void ProcessCreate(Int32[] playerNumbers, String[] playerLastNames, 
      Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS) 
     { 
      Int32 number, points; 
      String lastName; 
      if (playerCount < MAXPLAYERS) 
      { 
       number = GetPositiveInteger("\nCreate Player: please enter the player's number"); 
       if (GetPlayerIndex(number, playerNumbers, playerCount) == -1) 
       { 
        lastName = GetString("\nCreate Player: please enter the player's last name"); 
        points = GetPositiveInteger("\nCreate Player: please enter the player's points"); 
        InsertPlayer(number, lastName, points, playerNumbers, playerLastNames, playerPoints, ref playerCount); 
        Console.WriteLine("\nCreate Player: Number - {0}, Name - {1}, Points - {2}, created successfully", number, lastName, points); 
        Console.WriteLine(); 
       } 
       else 
        Console.WriteLine("\nCreate Player: the player number already exists"); 
      } 
      else 
       Console.WriteLine("\nCreate Player: the player roster is already full"); 

     } 

     //Inserts the player at the correct location in the tables based on order of 
     //ascending player number. Unless the insert location is at the end, this 
     //requires shifting existing players down in order to make room 
     static void InsertPlayer(Int32 number, String lastName, Int32 points, 
      Int32[] playerNumbers, String[] playerLastNames, Int32[] playerPoints, 
      ref Int32 playerCount) 
     { 
      /* PUTS DATA IN NEXT AVALIABLE SLOT 
      playerNumbers[playerCount] = number; 
      playerLastNames[playerCount] = lastName; 
      playerPoints[playerCount] = points; 
      playerCount++; 
      */ 

      // PUTS DATA IN PLAYER ASECENDING ORDER 
      Int32 insertIndex, shiftCount; 
      insertIndex = GetInsertIndex(number, playerNumbers, playerCount); 
      for (shiftCount = playerCount; shiftCount > insertIndex; shiftCount--) 
      { 
       playerNumbers[shiftCount] = playerNumbers[shiftCount - 1]; 
       playerLastNames[shiftCount] = playerLastNames[shiftCount - 1]; 
       playerPoints[shiftCount] = playerPoints[shiftCount - 1]; 
      } 
      playerNumbers[insertIndex] = number; 
      playerLastNames[insertIndex] = lastName; 
      playerPoints[insertIndex] = points; 
      playerCount++; 

     } 
     //Returns the index of the first player number in the table that is greater 
     //than the player number to be inserted 
     static Int32 GetInsertIndex(Int32 playerNumber, Int32[] playerNumbers, 
      Int32 playerCount) 
     { 
      Int32 index = 0; 
      bool found = false; 
      while (index < playerCount && found == false) 
       if (playerNumbers[index] > playerNumber) 
        found = true; 
       else 
        index++; 
      return index; 
     } 

     //Returns the index of the player number in the table 
     //or -1 if the number is not found 
     static Int32 GetPlayerIndex(Int32 playerNumber, 
      Int32[] playerNumbers, Int32 playerCount) 
     { 
      Int32 index = 0; 
      bool found = false; 
      while (index < playerCount && found == false) 
       if (playerNumbers[index] == playerNumber) 
        found = true; 
       else 
        index++; 
      if (found == false) 
       index = -1; 
      return index; 
     } 

     //Lists the players in the tables 
     static void ProcessList(Int32[] playerNumbers, String[] playerLastNames, 
      Int32[] playerPoints, Int32 playerCount) 
     { 

      if (playerCount > 0) 
      { 
       Console.WriteLine("\n{0,7} {1,-25}{2,6}\n", "Number", "Last Name", "Points"); 
       for (Int32 player = 0; player < playerCount; player++) 
        Console.WriteLine("{0,7} {1,-25}{2,6}", playerNumbers[player], playerLastNames[player], playerPoints[player]); 
      } 
      else 
       Console.WriteLine("\nList Players: the roster is empty"); 
     } 

     //Returns a positive integer 
     static Int32 GetPositiveInteger(String prompt) 
     { 
      Int32 n; 
      Console.WriteLine(prompt); 
      n = Int32.Parse(Console.ReadLine()); 
      while (n < 0) 
      { 
       Console.WriteLine("\nError: enter positive value"); 
       Console.WriteLine(prompt); 
       n = Int32.Parse(Console.ReadLine()); 
      } 
      return n; 
     } 

     //Returns a non-empty string 
     static String GetString(String prompt) 
     { 
      String returnString; 
      Console.WriteLine(prompt); 
      returnString = Console.ReadLine(); 
      while (returnString == "") 
      { 
       Console.WriteLine("\nError: must enter keyboard data"); 
       Console.WriteLine(prompt); 
       returnString = Console.ReadLine(); 
      } 
      return returnString; 
     } 
     // retrieve single value from an array 

     //static void ProcessRetrieve(Int32[] playerNumbers, String[] playerLastNames 
     //, Int32[] playerPoints, Int32 playerCount) 
     //{ 

     //} 

所以在這裏我開始檢索其作品,是,我可以通過修改與接下來的兩個函數的工作重複使用功能的單個條目的過程。

 static void ProcessRetrieve(Int32[] playerNumbers, String[] playerLastNames, 
      Int32[] playerPoints, ref Int32 playerCount, Int32 MAXPLAYERS) 
     { 
      int player;// Player number to find 
      int playerindex;//index of the player number in Array 
      if (playerCount < MAXPLAYERS) 
      { 
       player = GetPositiveInteger("\nRetrieve Player: please enter the player's number"); //JG I used the same mechanism when you are creating the record, when you create yu check if it exists 
       /** If it exists you say record exists in this case I said display the existing record**/ 
       playerindex = GetPlayerIndex(player, playerNumbers, playerCount); 
       if (playerindex != -1)// !-1 means Does not exist JG 
       { 

        Console.WriteLine("{0,7} {1,-25}{2,6}", playerNumbers[playerindex], playerLastNames[playerindex], playerPoints[playerindex]); 
        Console.WriteLine(); 
       } 
       else 
        Console.WriteLine("\nRetrieve Player: the player number does not exists"); 
      } 
      else 
       Console.WriteLine("\nRetrieve Player: the player does not exist in the roster"); 
     } 

現在這裏是我遇到的困難,如何使這個代碼更新,而不是找回我完全失去了這裏,我已經研究過許多文章,才發現如何改變通過數組代碼而不是用戶輸入

 static void ProcessUpdate(Int32[] playerNumbers, 
     string[] playerLastnames, Int32[] playerpoints, Int32 playerCounts, Int32 MAXPLAYERS) 
     { 
      int player;// Player number to find 
      int playerindex;//index of the player number in Array 
      if (playerCounts < MAXPLAYERS || playerCounts == MAXPLAYERS) 
      { 
       player = GetPositiveInteger("\nUpdate Player: please enter the player's number"); 
       playerindex = GetPlayerIndex(player, playerNumbers, playerCounts); 
       if (playerindex != -1) 
       { 

        Console.WriteLine("{0,7} {1,-25}{2,6}", playerNumbers[playerindex], playerLastnames[playerindex], playerpoints[playerindex]); 
        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"); 
     } 

    } 
} 
+0

你可以指向該代碼不工作?有太多的代碼來理解你需要什麼 –

+0

在GetMenuItem()中,你正在解析字符串中的字符。控制檯也有Console.ReadKey(),因此用戶永遠不能輸入超過1個字符。 – Measuring

+0

我已經編輯我的代碼來分離我的問題領域,我做了很多研究,並發現如何檢索,但沒有這樣的運氣來做同樣的更新和刪除,但刪除我知道我只需要替換值的空格,所以我真的需要知道的是如何通過用戶輸入更新值。 – Shahze123

回答

1

在您的代碼中,您已經擁有創建更新函數所需的構建塊。我們來看看這些是什麼。

  1. 您必須先問問球員號碼。您已經在ProcessUpdate方法中實現了這一點。

     player = GetPositiveInteger("\nUpdate Player: please enter the player's number"); 
    
  2. 您必須找到與玩家號碼匹配的數組索引。這也已經在ProcessUpdate方法中實現了。

     playerindex = GetPlayerIndex(player, playerNumbers, playerCounts); 
    
  3. 您將需要詢問用戶對新玩家數據。您已經在ProcessCreate方法中實現了這一點 - 只需要修改字符串以澄清我們現在要求更新的數據。

      lastName = GetString("\nUpdate Player: please enter the player's updated last name"); 
          points = GetPositiveInteger("\nUpdate Player: please enter the player's updated points"); 
    
  4. 最後,你必須把你剛剛從用戶到陣列中獲得數據。您已經在InsertPlayer中實現了這一點 - 我們只是注意在這裏使用正確的變量名稱。

    playerLastNames[playerindex] = lastName; 
        playerPoints[playerindex] = points; 
    


把所有這些組合起來(包括變量聲明和完整性檢查),您ProcessUpdate方法應該是這樣的:

static void ProcessUpdate(Int32[] playerNumbers, 
    string[] playerLastnames, Int32[] playerpoints, Int32 playerCounts, Int32 MAXPLAYERS) 
    { 
     int player;// Player number to find 
     int playerindex;//index of the player number in Array 

     String lastName; 
     int points; 

     if (playerCounts < MAXPLAYERS || playerCounts == MAXPLAYERS) 
     { 
      player = GetPositiveInteger("\nUpdate Player: please enter the player's number"); 
      playerindex = GetPlayerIndex(player, playerNumbers, playerCounts); 
      if (playerindex != -1) 
      { 
       lastName = GetString("\nUpdate Player: please enter the player's updated last name"); 
       points = GetPositiveInteger("\nUpdate Player: please enter the player's updated points"); 

       playerLastNames[playerindex] = lastName; 
       playerPoints[playerindex] = points; 

      } 
      else 
       Console.WriteLine("\nUpdate Player: the player number does not exists"); 
     } 
     else 
      Console.WriteLine("\nUpdate Player: the player does not exist in the roster"); 
     } 
    } 

一般來說,你的程序的結構不一個如何做對的例子(對不起)。但是,我不知道你的編程課程。您的課程是否已經涵蓋了結構,對象(面向對象編程)或其他「高級」數據類型(如字典),還是未來的主題?我知道你需要使用3個數組,但是已經在struct的幫助下,你可以顯着地清理你的代碼,同時仍然滿足需求。

+0

不,我們還沒有涉及適當的結構化,教授認爲,在他教我們如何使用類來清理我們的代碼之前,通過我們的不重要的編碼學習是不必要的,但是,感謝您的幫助,我知道我是在正確的軌道上只是不知道如何獲得新的數據到我的陣列 – Shahze123

0

下面我寫了一個關於如何通過用戶輸入更新玩家得分和名字的例子。 通過保持每個玩家相同索引中的數字,名稱和點的索引,我們可以跟蹤它們。

// Make sure the indexes of the arrays are aligned with the player array 
// (to keep track which points are for which player) 
int[] playerNums = new int[10]; 
string[] playerNames = new string[10]; 
int[] playerPoints = new int[10]; 

// We now add the user here (you do it somewhere from user input) 
playerNums[0] = 1; 
playerNames[0] = "Tim"; 
playerPoints[0] = 10; 

char key = Console.ReadKey(true).KeyChar; 
if (char.IsDigit(key)) 
{ 
    // We get the number from the char. 
    int inputNum = int.Parse(key.ToString()); 

    // We make sure the user isn't giving an index past the length of our arrays 
    // (which are 10 in size). 
    if (inputNum > -1 && inputNum < playerNums.Length - 1) 
    { 
     playerNames[inputNum] = "John"; // Tim now becomes John. 
     playerPoints[inputNum] += 5; // Increase John's score. 
    } 
} 

告訴我,如果這是你正在尋找的答案。

+0

我明白你在這裏做什麼,但這並不完全是我的意思,我希望能夠修改用戶通過我創建的「創建球員」功能進入的球員 – Shahze123

+1

無論如何,@ Shahze123可能會讓你在理解C#的基礎知識時遇到困難嗎?要知道這將有助於理解你的問題。 – elgonzo

+0

@elgonzo這將是正確的我是一個初學者編程的學生,我正在做這個任務,我明白了一點,但一些概念逃脫我。 – Shahze123

0

而不是數組使用字典如下所示,以便您不需要維護所有數組的任何索引,並且如果要搜索特定的元素,它是非常容易的。玩家號碼是每個玩家信息的關鍵,你可以選擇任何你想要的密鑰。 請參閱下面的鏈接瞭解詳情 http://www.dotnetperls.com/dictionary

命名空間播放器 { 類節目 {

static void Main(string[] args) 
    { 
     Dictionary<string, PlayerInfo> info = new Dictionary<string, PlayerInfo>(); 
     for (int i = 0; i < 10; i++) 
     { 
      Console.WriteLine("Enter Player Number"); 
      string playerNumber = Console.ReadLine(); 
      info.Add(playerNumber, new PlayerInfo()); 
      Console.WriteLine("Enter Player Name"); 
      info[playerNumber].PlayerName = Console.ReadLine(); 
      Console.WriteLine("Enter Player Points"); 
      info[playerNumber].points = Console.ReadLine(); 
     } 

     Console.WriteLine("Enter the player number to be deleted"); 
     string playerNumberToDelete = Console.ReadLine(); 
     info.Remove(playerNumberToDelete); 

     Console.WriteLine("Enter a player number to update"); 
     string playerNumberToUpdate = Console.ReadLine(); 
     Console.WriteLine("Enter Player Name"); 
     info[playerNumberToUpdate].PlayerName = Console.ReadLine(); 
     Console.WriteLine("Enter Player Points"); 
     info[playerNumberToUpdate].points = Console.ReadLine(); 

     Console.WriteLine("Enter player number dispaly deatils"); 
     string playerNumberToDisplay = Console.ReadLine(); 
     Console.WriteLine("Name " + info[playerNumberToDisplay].PlayerName); 
     Console.WriteLine("Points " + info[playerNumberToDisplay].points); 
    } 
} 
class PlayerInfo 
{ 
    public string PlayerName; 
    public string points; 
} 

}

+0

OP必須使用3陣列每個他的任務。 – Measuring

+0

@Kumareshan,因爲我的問題表明我需要使用三個數組,可悲的是我無法獲得更新仍然工作,但檢索工作沒有問題,有誰知道如何修改它我已經通過了每一行我自己的代碼,看看我是否可以做到這一點,但仍然沒有什麼我希望我的'ProcessCreate'會有東西,但它是完全相同的,因爲它不會工作。 – Shahze123

+0

您的ProcessUpdate如下所示 – Kumareshan

0

你ProcessUpdate應看看下面

 static void ProcessUpdate(Int32[] playerNumbers, string[] playerLastnames, Int32[] playerpoints, Int32 playerCounts, Int32 MAXPLAYERS) 
     { 
      int player;// Player number to find 
      int playerindex;//index of the player number in Array 
      if (playerCounts <= MAXPLAYERS) 
      { 
       player = GetPositiveInteger("\nUpdate Player: please enter the player's number"); 
       playerindex = GetPlayerIndex(player, playerNumbers, playerCounts); 
       if (playerindex != -1) 
       { 
        string lastName = GetString("\nCreate Player: please enter the player's last name"); 
        int points = GetPositiveInteger("\nCreate Player: please enter the player's points"); 
        playerLastnames[playerindex] = lastName; 
        playerpoints[playerindex] = points; 
        Console.WriteLine("{0,7} {1,-25}{2,6}", playerNumbers[playerindex], playerLastnames[playerindex], playerpoints[playerindex]); 
        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"); 
     }