如何打开随机字符串数组';单词';放入由字符串';s字符C#?
好的,所以我正在创造一个吊死人游戏(跛脚,我知道,但我必须'从某处开始')。我已经成功地从一个文本文件中提取了约30个随机单词到一个变量中,并且可以在屏幕上以随机顺序正确地显示该单词(只是为了测试并确保该变量以随机顺序获取整个单词) 但我需要将该字符串拆分为单个字符,以便“清空”用户要“猜测”的字母。我认为数组是实现这一点的最佳方式——再加上一个while循环,该循环将在角色运行时运行!=空如何打开随机字符串数组';单词';放入由字符串';s字符C#?,c#,arrays,string,char,C#,Arrays,String,Char,好的,所以我正在创造一个吊死人游戏(跛脚,我知道,但我必须'从某处开始')。我已经成功地从一个文本文件中提取了约30个随机单词到一个变量中,并且可以在屏幕上以随机顺序正确地显示该单词(只是为了测试并确保该变量以随机顺序获取整个单词) 但我需要将该字符串拆分为单个字符,以便“清空”用户要“猜测”的字母。我认为数组是实现这一点的最佳方式——再加上一个while循环,该循环将在角色运行时运行!=空 using System; using System.Collections.Generic; usin
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(“文件名”)方式但这在这里不起作用
编辑
我不明白为什么有人会投票否决这个问题。lol您可以通过索引访问字符串中的任何字符,因此您可以将字符串视为字符数组: 例如,如以下代码段:
string word = "word";
char w1 = word[0];
Console.WriteLine(w1);
这里有几点(首先,你有一个很好的开始):
myWordArrays.Length
设置lineCount
变量file.ReadAllLines()
在文件读取完毕后关闭该文件,这样您就可以使用已有的文件了Length
属性而言,字符串本身可以被视为数组。还可以隐式地对其进行迭代,如下所示:
foreach (char letter in myWordArrays[activeWord])
{
// provide a blanked-out letter for each char
}
您可以将代码简化一点,如下所示。以前,您的
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#中的字符串可以被视为可枚举对象,因此,如果需要对字符串的某些部分进行变异,则只应使用字符数组。多亏了Sven,我才能够找到它,并且能够在其中添加一些内容!!我发布这篇文章是为了让其他新手从新手的角度理解:
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
}
}
}
var chars=“abcd”.ToArray()但是单词是随机选择的,而且单词的长度是不同的。我不知道要移动到数组中的“word”是什么,而且我似乎只能让activeWord在元素myWordArrays[activeWord]myWordArrays[activeWord].ToArray()
:)中作为“写入控制台”来运行。Close()的第二个问题使用using语句。它基本上在using语句中保持文件打开@thinklarge-谢谢。是的,但是单词是随机选择的,因此我不知道每个数组有多少个元素。我是否必须首先获取arrayVariable.Length,然后分别引用每个元素?@Newbie每个字符串都有其长度,由Length
属性指定。在我发布的截图中,word.Length
将返回其exect长度4。所以从这个角度来看,它和数组非常相似。如果您需要“true”char[]
object,您可以使用String.tocharray
方法。对,但是为了给每个字母写空格,我需要知道要写多少个元素,对吗?或者我可以只做Console.WriteLine(myWordArray[activeWord].length)?@Newbie好吧,如果您只需要输出包含空格的字符串并具有精确长度,那么您可以使用适当的构造函数对字符串进行符号重复和重复计数:Console.WriteLine(新字符串('',myWordArray[activeWord].length))代码>。谢谢。事实上,这正是我在“foreach”循环之后显示单词中字母数量的方法,以便为字母的每个表示形式显示下划线。非常感谢。太棒了,先生。这太奇怪了-我知道我试过myWordArrays.Length,但不知什么原因都没用。哈哈。所以基本上,我现在可以去掉整个lineCount变量了。:)关于foreach的问题:您(char-letter…)中的“字母”是您创建的变量吗?或者它是c#中的一种实际类型?letter
只是在foreach
闭包中创建的一个变量。它只会存在于支架内,非常酷。我只是用了这个,它工作了,但是当它显示在控制台上时,我得到了f_u(然后在新行中)o_u(新行)r_u(新行)t_u-这个词是fort。哈哈。现在我需要弄清楚如何用空格替换每个字母。不过别告诉我。伊玛试着自己解决这个问题。非常感谢你@新手提示:除了Console
上的WriteLINE()
之外,还有其他方法可以解决您的(换行)问题……是的,所以我找到了答案。我做WriteLine只是因为我已经习惯了,我把每个“字母”+“连在一起”,所以我只是删除了字母并保留了“”。另外,我使用的是Console.Write(),这样它就不会每次都出现在新的行中。谢谢你没有把它送人。另外,我现在有了我的程序,它将实际读取单词的长度,并询问“你能猜出这个‘x’字母单词是什么吗”并且长度正确显示!!令人惊叹的!哈哈。这是一个让人自豪的愚蠢的节目,但对我来说,这是一个愚蠢的节目。:)再次感谢!我已经试过了,但它说它不能隐式地将int转换为string——它说activeWord自动地是一个int(这就是为什么我将它声明为一个into来启动)??