2013-03-13 45 views
0

问题是我发现我的方法需要从多个char变量中构建一个char数组 - > char []。有人能指引我走向正确的方向吗?添加char到char数组问题,hangman游戏

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

namespace ConsoleApplication1 
{ 
class Program 
{ 
    string[] wordList = { 
          "Baseball", "Tackle", "Dubstep", "Ignorance", "Limitation", "Sausage", 
          "Destruction", "Patriot", "Computing", "Assembly", "Coding", "Hackers", 
          "Football", "Downward" 
         }; 
    static void Main(string[] args) 
    { 
     int guessRemain = 7; 
     int wordSel = GenerateRandom(); 
     Program o = new Program(); 


     char[] wordChar = o.wordList[wordSel].ToLower().ToCharArray(); 
     int MAX_BUF = wordChar.Length; 


     Console.WriteLine("\nHANGMAN v 1.0\n\n\n\n"); 
     char[] userInput = PromptUserEntry(); 
     char[] solution = ScanForMatchingLetter(wordChar, MAX_BUF, userInput); 


     Console.Read(); 
    } 
    private static char ScanForMatchingLetter(char[] wordChar, int MAX_BUF, char[] userInput) 
    { 
     char[] solution = new char[MAX_BUF]; 
     for (int i = 0; i < MAX_BUF; ++i) 
     { 
      if (userInput[0] == wordChar[i]) 
      { 
       solution[i] = userInput[0]; 

      } 


     } 
     return solution; 
    } 
    private static char[] PromptUserEntry() 
    { 
     Console.WriteLine("Pick a letter:"); 
     char[] userInput = Console.ReadLine().ToCharArray(); 
     return userInput; 
    } 
    private static void DisplayGuessLetterLine(char[] solution) 
    { 

     Console.Write(solution); 

    } 
    private static int GenerateRandom() 
    { 
     Random randNum = new Random(); 
     int wordSel = randNum.Next(0, 13); 
     return wordSel; 
    } 

} 
} 

我在这里有一个返回类型的问题;返回类型被指定为char,但是我返回一个char []。

+4

如果您需要可变长度存储,请不要使用数组。 'List '可能对你更好。 – Oded 2013-03-13 20:38:12

+0

为什么当你实际需要一个char []和方法('ScanForMatchingLetter')时,你甚至会返回一个字符char []? – 2013-03-13 20:39:41

+0

你所有的函数都是类中的静态函数,但是你的单词数组是一个实例变量。你也可以将'static'应用于'wordList',而不是像你所做的那样创建一个Program类的实例。或者,从一切中删除'static',使一个非静态方法'Run',并在'Main'中简单地执行'Program o = new Program(); o.Run();' – Steve 2013-03-13 20:41:01

回答

1

在您使用的字符数组每一个实例,与

List<char> 

列表允许您添加和随意删除,reboxing你对底层数组,这样你就不必担心更换它们关于它。

我已经用您的决议更新了我的答案。当你只有一个用户输入时,使用List来代替char数组,只返回单个字符,而不是数组。我希望这可以帮助并解决您的问题

class Program 
{ 
    readonly string[] wordList = { 
         "Baseball", "Tackle", "Dubstep", "Ignorance", "Limitation", "Sausage", 
         "Destruction", "Patriot", "Computing", "Assembly", "Coding", "Hackers", 
         "Football", "Downward" 
        }; 
    static void Main(string[] args) 
    { 
     int guessRemain = 7; 
     int wordSel = GenerateRandom(); 
     Program o = new Program(); 


     List<char> wordChar = o.wordList[wordSel].ToLower().ToList(); 
     int MAX_BUF = wordChar.Count; 


     Console.WriteLine("\nHANGMAN v 1.0\n\n\n\n"); 
     char userInput = PromptUserEntry(); 
     List<char> solution = ScanForMatchingLetter(wordChar, MAX_BUF, userInput); 


     Console.Read(); 
    } 
    private static List<char> ScanForMatchingLetter(List<char> wordChar, int MAX_BUF, char userInput) 
    { 
     List<char> solution = new char[MAX_BUF].ToList(); 
     for (int i = 0; i < MAX_BUF; ++i) 
     { 
      if (userInput == wordChar[i]) 
      { 
       solution[i] = userInput; 

      } 


     } 
     return solution; 
    } 
    private static char PromptUserEntry() 
    { 
     Console.WriteLine("Pick a letter:"); 
     char userInput = Console.ReadLine()[0]; 
     return userInput; 
    } 
    private static void DisplayGuessLetterLine(List<char> solution) 
    { 

     Console.Write(solution); 

    } 
    private static int GenerateRandom() 
    { 
     Random randNum = new Random(); 
     int wordSel = randNum.Next(0, 13); 
     return wordSel; 
    } 
} 
+0

好吧我会尝试 – 2013-03-13 20:52:22

+0

@UmairAslam请参阅我的更新。我认为它会帮助你很多 – 2013-03-13 21:01:18

+0

,但它仍然会给出转换的一些错误。但是这次它不能将字符串转换为字符。 什么是类似于tocharArray()的代码?在通用? 因为我想。 tolist()无法正常工作 – 2013-03-13 21:11:40