问题是我发现我的方法需要从多个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 []。
如果您需要可变长度存储,请不要使用数组。 'List'可能对你更好。 –
Oded
2013-03-13 20:38:12
为什么当你实际需要一个char []和方法('ScanForMatchingLetter')时,你甚至会返回一个字符char []? – 2013-03-13 20:39:41
你所有的函数都是类中的静态函数,但是你的单词数组是一个实例变量。你也可以将'static'应用于'wordList',而不是像你所做的那样创建一个Program类的实例。或者,从一切中删除'static',使一个非静态方法'Run',并在'Main'中简单地执行'Program o = new Program(); o.Run();' – Steve 2013-03-13 20:41:01