C# 有没有办法在For循环中保存字符数组?
我想创造一个简单的刽子手,我被卡住了:/。 下面是检测数组中所有字符的简单代码。 我需要一些方法来保存它,然后再写它。 我在代码中添加注释以提高可读性和保存位置。 最后我需要写下来。 在代码方面有什么我可以做得更好的吗?我是个新手C# 有没有办法在For循环中保存字符数组?,c#,C#,我想创造一个简单的刽子手,我被卡住了:/。 下面是检测数组中所有字符的简单代码。 我需要一些方法来保存它,然后再写它。 我在代码中添加注释以提高可读性和保存位置。 最后我需要写下来。 在代码方面有什么我可以做得更好的吗?我是个新手 public static void Main(string[] args) { char[] array; randomWord = "apple".ToCharArray(); Console.WriteLine(randomWord);
public static void Main(string[] args)
{
char[] array;
randomWord = "apple".ToCharArray();
Console.WriteLine(randomWord);
while (guessing == true) {
Console.WriteLine();
userinput = char.Parse(Console.ReadLine());
for (int i = 0; i < randomWord.Length; i++)
{
if (randomWord[i].ToString().Contains(userinput))
{
Console.Write(userinput);
//add to array
enter code here
}
else
{
//add to array
enter code here
Console.Write("_ ");
}
}
//and here Write whole array
for(int g = 0; g < array.Lenght; g++){
Console.Write(array[g]);
}
}
publicstaticvoidmain(字符串[]args)
{
字符[]数组;
randomWord=“apple”。ToCharArray();
控制台写入线(随机字);
while(猜测==真){
Console.WriteLine();
userinput=char.Parse(Console.ReadLine());
for(int i=0;i
使用比阵列更灵活的阵列:
public static void Main(string[] args)
{
List<char> array = new List<char>();
randomWord = "apple".ToCharArray();
Console.WriteLine(randomWord);
while (guessing == true) {
Console.WriteLine();
userinput = char.Parse(Console.ReadLine());
for (int i = 0; i < randomWord.Length; i++)
{
if (randomWord[i].ToString().Contains(userinput))
{
Console.Write(userinput);
//add to array
array.Add(randomWord[i]);
}
else
{
//it's not clear what you want to add to here?
Console.Write("_ ");
}
}
//and here Write whole array
//use a foreach
foreach(char c in array ){
Console.Write(c);
}
//your missing a brace
}
}
publicstaticvoidmain(字符串[]args)
{
列表数组=新列表();
randomWord=“apple”。ToCharArray();
控制台写入线(随机字);
while(猜测==真){
Console.WriteLine();
userinput=char.Parse(Console.ReadLine());
for(int i=0;i //不清楚你想在这里添加什么?
控制台。写(“”);
}
}
//这里写下整个数组
//使用foreach
foreach(数组中的字符c){
控制台。写入(c);
}
//你缺了一个支架
}
}
@danielshillock@Liam Ah!我认为在这种情况下,关于询问家庭作业问题的要点仍然需要遵循。这不是我的家庭作业。我自己学习。我甚至不在高中Alex,有人告诉过你使用char[]?我不会…所以对我来说这是一个x-y问题。为什么不简单地使用字符串:?如果这不是家庭作业…你将很少在c#“//不清楚你想在这里添加什么?”-我猜他正在尝试打印“hang man”的另一部分,因为字母不是单词的一部分?我想添加“#”有些事情需要做一些空间,但已经解决了,谢谢:)