好吧,所以我正在制作一个挂人游戏(Lame,我知道,但我必须'从某个地方开始)。我已成功将约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”)方式。 - 但这不适用于此。
修改
为什么有人会把这个问题投反对票超出我的意思。洛尔
答案 0 :(得分:1)
您可以通过索引访问字符串中的任何字符,因此您可以将字符串视为字符数组:
例如,像这个代码段:
string word = "word";
char w1 = word[0];
Console.WriteLine(w1);
答案 1 :(得分:1)
这里有几点(首先,你有一个良好的开端):
myWordArrays.Length
设置lineCount
变量File.ReadAllLines()
完成阅读后会关闭文件,因此您可以使用已有的文件。在按索引访问和访问其Length
属性方面,字符串本身可以像数组一样对待。还有能力隐式迭代它:
foreach (char letter in myWordArrays[activeWord])
{
// provide a blanked-out letter for each char
}
答案 2 :(得分:0)
您可以按如下方式简化代码。以前,您的activeWord
变量是一个整数,因此无法转换为字符数组。
static void Main(string[] args)
{
String[] myWordArrays = File.ReadAllLines("WordList.txt");
Random random = new Random();
string activeWord = myWordArrays[random.next(myWordArrays.Length)];
char[] chars = activeWord.ToCharArray();
}
但是,C#中的字符串可以视为可枚举对象,因此如果需要改变字符串的部分内容,则应该只使用字符数组。
答案 3 :(得分:0)
感谢Sven,我能够弄明白,我也可以添加一些东西!我将这篇文章发布给其他新手,以便从newb的角度理解:
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)
{
printWord();
}
/*I created a method to perform this, so that I can have the program stay open for
either 1) a new game or 2) just to see if I could do it. It works!*/
private static void printWord()
{
String[] myWordArrays = File.ReadAllLines("WordList.txt");
Random randomWord = new Random();
//int lineCount = File.ReadLines("WordList.txt").Count();
//The line below fixed my previous issue of double-reading file
int activeWord = randomWord.Next(0, myWordArrays.Length);
string userSelection = "";
Console.WriteLine("Are you Ready to play Hangman? yes/no: ");
userSelection = Console.ReadLine();
if(userSelection == "yes")
{
/*This runs through the randomly chosen word and prints an underscore in
place of each letter - it does work and this is what fixed my
previous issue - thank you Sven*/
foreach(char letter in myWordArrays[activeWord])
{
Console.Write("_ ");
}
//This prints to the console "Can you guess what this 'varyingLength' letter word is?" - it does work.
Console.WriteLine("Can you guess what this "+ myWordArrays[activeWord].Length +" letter word is?");
Console.ReadLine();
}
//else if(userSelection=="no") -- will add more later
}
}
}