【发布时间】:2015-07-22 14:45:43
【问题描述】:
好的,所以我正在创建一个刽子手游戏(跛脚,我知道,但我必须从某个地方开始)。我已经成功地将大约 30 个随机单词从文本文件中提取到一个变量中,并且可以在屏幕上以随机顺序正确显示该单词(只是为了测试并确保该变量以随机顺序获取整个单词)。
但我需要将该字符串分解为单个字符,以便“删除”用户“猜测”的字母。我认为数组是做到这一点的最佳方式——再加上一个 while 循环,该循环将在字符 != null 时运行。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hangman
{
class Program
{
static void Main(string[] args)
{
String[] myWordArrays = File.ReadAllLines("WordList.txt");
Random randomWord = new Random();
int lineCount = File.ReadLines("WordList.txt").Count();
int activeWord = randomWord.Next(0, lineCount);
/*CharEnumerator activeWordChar = activeWord; --- I have tried this,
but it says "Cannot implicitly convert type 'int' to 'System.CharEnumerator'
--- while redlining "activeWord." */
/*CharEnumerator activeWordChar = activeWord.ToString
-- I have tried this but it says "Cannot convert method group 'ToString' to
non-delegate type 'System.CharEnumerator'. Did you intend to invoke the method?
I also tried moving the declaration of activeWordChar below the 'writeline'
that displays the word properly to the console.
I have even tried to create a Char[] activeWordChar = activeWord.toCharArray; But this doesn't work either.
*/
//I'm using this writeline "the word for this game is: " ONLY to test that the
//system is choosing random word **end comment
Console.WriteLine("The Word for this game is: " + myWordArrays[activeWord]);
//Console.WriteLine("The Characters are like this: " + activeWordChar[]);
//my attempt at printing something, but it doesn't work. :(
Console.ReadLine();
}
}
}
我愿意参考参考资料以便自己弄清楚,但我有点卡在这里。
另外,如果需要,我如何关闭我打开的文件,以便以后可以在程序中访问它?我只学习了 'variable.Close();' 的 StreamReader("filename") 方式- 但这在这里行不通。
编辑
为什么有人会否决这个问题,这超出了我的理解。哈哈
【问题讨论】:
-
var chars = "abcd".ToArray(); -
但是单词是随机选择的,单词的长度不等。我不知道要移动到数组中的“单词” - 如果它在元素 myWordArrays[activeWord] 中,我似乎只能让 activeWord 充当“写入控制台”
-
myWordArrays[activeWord].ToArray():) -
对于 .Close() 的第二个问题,请改用 using 语句。它基本上在 using 语句中保持文件打开。 msdn.microsoft.com/en-us/library/yh598w02.aspx
-
@thinklarge - 谢谢。