2016-02-23 92 views
0

我目前正在一个小型项目中创建一个hang子手游戏作为控制台应用程序。目前,我遇到了问题,因为我想为要猜测的单词创建一个数组。这个词不是随机的,并且始终是相同的。在这种情况下,它是“米勒”。我想为“miller”创建一个数组,并有一个循环来显示单词字符数组中的每个字母。当信件被正确猜测时,我想显示正确的字母,如果没有显示特殊字符“*”代替。所以,例如,这个词是“米勒”,并且将首先显示为“******”。当用户正确猜对一个字母时,一个特殊字符会成为正确的猜字母。因此,说用户猜测“我”...显示的单词将是“*我****”。假设他猜测下一个“l”......所显示的单词将会是“* ill **”。我会如何去做这件事?这是我的代码到目前为止......我已经创建了试图为此创建一个数组。对不起,如果这看起来微不足道,但我是编程新手,只需要一些指导和帮助。Hang子手控制台应用程序

TLDR; 我需要此代码的帮助。我正在尝试创建一个包含'miller'的数组,并且我希望能够在用户猜测字母正确地将特殊字符“*”的显示更改为正确猜测的字母时调用该数组。我被告知要做一个for循环是最好的路线,但我有点迷路。帮帮我?

static void Main(string[] args) 
    {    
     char[] guessed = new char[26]; 

     char[] word = "miller".ToCharArray(); 

     char guess; 

     int score = 0, index = 0; 
     Console.WriteLine("******"); 

     for (int i = 0; i < 10; i++) 
     { 

      Console.Write("Please enter a letter to guess: "); 

      guess = char.Parse(Console.ReadLine()); 

      if (guess == l1 || guess == l2 || guess == l3 || guess == l4 || guess == l5 || guess == l6) 
      { 
       Console.WriteLine("Your guess is correct."); 
       guessed[index] = guess; 
       index++; 
      } 
      else 
      { 
       Console.WriteLine("Your guess is incorrect."); 
       score++; 
      } 



     } 

     Console.WriteLine("Your score is " + score); 

     Console.ReadLine(); 

    } 
} 

}

+0

作为一个方面说明,我忘了说,我原来把每个字母定义为一个变量。所以,变量“l1”将包含“m”,“l2”将包含“i”等等。它们也被作为char变量。我忘了提及并在发布前改变它。谢谢! – user5927290

+1

首先将问题分解成更小的部分。这个问题太大了,没有人为你做这个帮助就没有帮助 –

+0

我不这么认为。我唯一需要的是帮助制定数组以及如何创建for循环来更改特殊字符。我不需要有人为我写代码。一个简单的伪代码可以帮助。 – user5927290

回答

0

包括using System.Linq;在你的文件,这里是你的for循环。

for (int 1 = 0; i < 10; i++) 
{ 
    Console.Write("Please enter a letter to guess: "); 
    char guess = char.Parse(Console.ReadLine()); 

    if(word.Contains(guess))//checks if the word is is the array word 
    { 
     //the guess was correct. 
     Console.WriteLine("Your guess is correct."); 
     guessed[index] = guess; 
     index++; 
    } 
    else 
    { 
     //the guess was wrong. 
     console.WriteLine("Your guess is incorrect."); 
     .... 
    } 
} 

希望这会有所帮助。

0

看起来你很接近那里。两件事情,我不确定是if语句“猜测== || L1猜测== L2”等等等等......这可与处理:

if(word.Contains(guess)) 

其次,你需要另一个用于循环通过正确的猜测字母并放置它们。

for循环填写字母代替星号: 把它在为始(INT I = 10 ...)

for (int j = 0; j < word.Length; j++) 
{ 
    if (guessed.Contains(word[j])) 
    { 
     Console.Write(word[j]); 
    } 
    else 
    { 
     Console.Write("*"); 
    } 
} 
1

我不知道这是不是你在找什么因为,也许你下次应该更具体一些......但我试图为你的问题创建一个解决方案。

static void Main(string[] args) 
    { 
     char[] guessed = new char[26]; 
     char[] testword = "******".ToCharArray(); 
     char[] word = "miller".ToCharArray(); 
     char[] copy = word; 

     char guess; 

     int score = 0, index = 0; 
     Console.WriteLine(testword); 

     for (int i = 0; i < 10; i++) 
     { 

      Console.Write("Please enter a letter to guess: "); 

      guess = char.Parse(Console.ReadLine()); 
      bool right = false; 
      for (int j = 0; j < copy.Length; j++) 
      { 
       if (copy[j] == guess) 
       { 
        Console.WriteLine("Your guess is correct."); 
        testword[j] = guess; 
        guessed[index] = guess; 
        index++; 
        right = true; 
       } 
      } 

      if (right != true) 
      { 
       Console.WriteLine("Your guess is incorrect."); 
       score++; 
      } 
      else 
      { 
       right = false; 
      } 

      Console.WriteLine(testword); 


     } 

     Console.WriteLine("Your score is " + score); 

     Console.ReadLine(); 

    }