从列表中选择随机字? [英] Pick random word from list?
问题描述
遇到问题I'm从另一个文件列表中选择一个随机单词。
其实我不能甚至得到它选择的任何一句话。我不确定如何将两个文件连这么说。
希望有人能帮忙,我是初学者所以请解释尽可能轻松:)
我有2个文件,一个被称为Program.cs中和另一种是所谓WordList.cs
I'm会贴上我的代码,但首先小剪断了有问题I'm。我只是不容弄清楚如何编写代码正确
下面是被称为匹克字小部分:
// PICK WORD
静态字符串pickWord()
{
串returnword =;
TextReader的文件=新的StreamReader(字);
串fileLine = file.ReadLine();
随机randomGen =新的随机();
returnword =字[randomGen.Next(0,words.Count - 1)];
返回returnword;
}
这是Program.cs中所有的代码
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
:使用System.IO;
类刽子手
{
静态无效的主要(字串[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title =C#刽子手
Console.WriteLine(欢迎来到C#刽子手!);
// MENU
INT MenuChoice = 0;
,而(!MenuChoice = 4)
{
Console.Write(\\\
\t1)添加词语);
Console.Write(\\\
\t2)显示的单词列表);
Console.Write(\\\
\t3)播放);
Console.Write(\\\
\t4)Quit\\\
\\\
);
Console.Write(\\\
\tChoose 1-4:); //选择MENY项目
MenuChoice = Convert.ToInt32(到Console.ReadLine());
单词表显示=新单词表();
开关(MenuChoice)
{
案例1:
Console.Write(\\\
\tAdd一个word\\\
\\\
);
变种插入=到Console.ReadLine();
showing.AddWord(插入);
Console.Write(words\\\
\\\
的\\\
\tList);
showing.ListOfWords();
中断;
案例2:
Console.Write(words\\\
\\\
的\\\
\tList);
showing.ListOfWords();
中断;
案例3://游戏运行
INT numGuessesInt = -1;
,而(numGuessesInt == -1)
{
/ *设置用户有猜词* /
pickNumGuesses猜的次数(REF numGuessesInt) ;
}
/ *随机选择一个字* /
串字= pickWord();
/ *创建人物的名单,将显示* /
名单,LT;焦炭> guessedLetters =新的List<&烧焦GT;();
布尔解决= FALSE;
而(解决==假)
{
/ *显示的字符串基于该用户的正确的猜测用户。
*如果没有什么是正确的字符串将返回_ _ _* /
串wordToDisplay = displayWord(guessedLetters,字);
/ *如果返回的字符串包含_字符,所有的
*正确的字母尚未猜到了,所以检查,如果用户
*已经失去了,通过检查numGuessesLeft小于1 * /
如果
{
=解决真(wordToDisplay.Contains(_)!);
Console.WriteLine(!你赢得词是+字);
/ *检查如果用户想再次发挥。如果他们这样做,
*则解决了设置为true,将结束循环,
*否则,checkIfPlayAgain将关闭程序* /
checkIfPlayAgain()。
}
,否则如果(numGuessesInt< = 0)
{
解决= TRUE;
Console.WriteLine(你失去了词是+字);
checkIfPlayAgain();
}
,否则
{
/ *如果用户没有赢得或失去,拨打guessLetter,
* 1 * / $ B显示的话,减去猜测$ b guessLetter(guessedLetters,文字,wordToDisplay,楼盘numGuessesInt);
}
}
中断;
案例4:
Console.WriteLine(\\\
\tEnd游戏\\\
\\\
?)
中断;
默认:
Console.WriteLine(对不起,无效的选择);
中断;
}
}
}
// ******猜测PICK号码******
上配置静态无效pickNumGuesses(REF INT numGuessesInt)
{
串numGuessesString =;
Console.WriteLine(选择了一些猜测);
numGuessesString =到Console.ReadLine();
试
{
numGuessesInt = Convert.ToInt32(numGuessesString);
如果((numGuessesInt<!= 20安培; numGuessesInt> = 1))
{
抛出新的异常();
}
}
赶上(例外)
{
numGuessesInt = -1;
Console.WriteLine(错误:猜测无效号码);
}
}
// PICK WORD
静态字符串pickWord()
{
串returnword =;
TextReader的文件=新的StreamReader(字);
串fileLine = file.ReadLine();
随机randomGen =新的随机();
returnword =字[randomGen.Next(0,words.Count - 1)];
返回returnword;
}
// ******显示字******
静态字符串displayWord(列表<焦炭> guessedCharacters ,串字)
{
串returnedWord =;
如果(guessedCharacters.Count == 0)
{
的foreach(字符字字母)
{
returnedWord + =_;
}
返回returnedWord;
}
的foreach(字字符字母)
{
布尔letterMatch = FALSE;
的foreach(在guessedCharacters CHAR字符)
{
如果(字符==字母)
{
returnedWord + =字符+;
letterMatch = TRUE;
中断;
}
,否则
{
letterMatch = FALSE;
}
}
如果(letterMatch ==假)
{
returnedWord + =_;
}
}
返回returnedWord;
}
// ******猜字母******
静态无效guessLetter(列表<焦炭> guessedCharacters ,串词,串wordToDisplay,文献INT numGuessesLeft)
{
包含字母=;
的foreach(在guessedCharacters字符字母)
{
字母+ =+字母;
}
Console.WriteLine(猜一个字母);
Console.WriteLine(孰料快报:+字母);
Console.WriteLine(左猜测:+ numGuessesLeft);
Console.WriteLine(wordToDisplay);
串猜测=到Console.ReadLine();
字符guessedLetter ='A';
试
{
guessedLetter = Convert.ToChar(猜测);
如果(!Char.IsLetter(guessedLetter))
{
抛出新的异常();
}
}
赶上(例外)
{
Console.WriteLine(错误:无效信选择);
// guessLetter(guessedCharacters,文字,wordToDisplay,楼盘numGuessesLeft);
}
布尔重复= FALSE;
的for(int i = 0; I< guessedCharacters.Count;我++)
{
如果(guessedCharacters [I] == guessedLetter)
{
控制台。的WriteLine(错误:无效信选择);
复读= TRUE;
// guessLetter(guessedCharacters,文字,wordToDisplay,楼盘numGuessesLeft);
}
}
如果(重复==假)
{
guessedCharacters.Add(guessedLetter);
numGuessesLeft - = 1;
}
}
// ******检查是否有玩家想再次发挥。 ******
静态无效checkIfPlayAgain()
{
Console.WriteLine(你想再次发挥(Y / N)?);
串playAgain =到Console.ReadLine();
如果(playAgain ==n个)
{
Environment.Exit(1);
}
}
}
和这里是代码WordList.cs
使用系统;
使用System.Collections.Generic;
类单词表
{
名单,LT;字符串>字=新的List<串GT;();
公共无效ListOfWords()
{
字词。(测试); //包含:测试
字词。(狗); //包含:测试,狗
words.Insert(1,狗屎); //包含:测试,狗屎,狗
words.Sort();
的foreach(文字串字)//显示验证
{
Console.WriteLine(字);
}
}
公共无效AddWord(字符串值){
字词。(值);
}
}
我已经做了一些修改你的代码。代码工作,但现在还远未完善。
你的解决方案有两个文件的Program.cs
和 Wordlist.cs
,它看起来像这样
的Program.cs
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
:使用System.IO;
公共类刽子手
{
/ *
*在你的代码的一些注意事项:
*使用命名约定的方法和字段,即方法的名字开始以大写字母
*使用改性剂的方法,也就是私人的,公共的,在方法声明保护
* make变量私有,如果你使用它们的几种方法
*和最后:看书在C#
*
* /
私有静态词库的话;
私人静态随机randomGen =新的随机();
公共静态无效的主要(字串[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title =C#刽子手
Console.WriteLine(欢迎来到C#刽子手!);
initializeWordList();
// MENU
INT MenuChoice = 0;
,而(!MenuChoice = 4)
{
Console.Write(\\\
\t1)添加词语);
Console.Write(\\\
\t2)显示的单词列表);
Console.Write(\\\
\t3)播放);
Console.Write(\\\
\t4)Quit\\\
\\\
);
Console.Write(\\\
\tChoose 1-4:); //选择MENY项目
MenuChoice = Convert.ToInt32(到Console.ReadLine());
开关(MenuChoice)
{
案例1:
Console.Write(\\\
\tAdd一个word\\\
\\\
);
变种插入=到Console.ReadLine();
字词。(插入);
Console.Write(words\\\
\\\
的\\\
\tList);
的foreach(字符串u字)//显示验证
Console.WriteLine(W);
中断;
案例2:
Console.Write(words\\\
\\\
的\\\
\tList);
的foreach(字符串u字)//显示验证
Console.WriteLine(W);
中断;
案例3://游戏运行
INT numGuessesInt = -1;
,而(numGuessesInt == -1)
{
/ *设置用户有猜词* /
pickNumGuesses猜的次数(REF numGuessesInt) ;
}
/ *随机选择一个字* /
串字= PickWord();
/ *创建人物的名单,将显示* /
名单,LT;焦炭> guessedLetters =新的List<&烧焦GT;();
布尔解决= FALSE;
而(解决==假)
{
/ *显示的字符串基于该用户的正确的猜测用户。
*如果没有什么是正确的字符串将返回_ _ _* /
串wordToDisplay = displayWord(guessedLetters,字);
/ *如果返回的字符串包含_字符,所有的
*正确的字母尚未猜到了,所以检查,如果用户
*已经失去了,通过检查numGuessesLeft小于1 * /
如果
{
=解决真(wordToDisplay.Contains(_)!);
Console.WriteLine(!你赢得词是+字);
/ *检查如果用户想再次发挥。如果他们这样做,
*则解决了设置为true,将结束循环,
*否则,checkIfPlayAgain将关闭程序* /
checkIfPlayAgain()。
}
,否则如果(numGuessesInt< = 0)
{
解决= TRUE;
Console.WriteLine(你失去了词是+字);
checkIfPlayAgain();
}
,否则
{
/ *如果用户没有赢得或失去,拨打guessLetter,
* 1 * / $ B显示的话,减去猜测$ b guessLetter(guessedLetters,文字,wordToDisplay,楼盘numGuessesInt);
}
}
中断;
案例4:
Console.WriteLine(\\\
\tEnd游戏\\\
\\\
?)
中断;
默认:
Console.WriteLine(对不起,无效的选择);
中断;
}
}
}
私有静态无效initializeWordList()
{
字=新单词表();
字词。(测试); //包含:测试
字词。(狗); //包含:测试,狗
words.Insert(1,狗屎); //包含:测试,狗屎,狗
words.Sort();
}
// ******猜测PICK号码******
私有静态无效pickNumGuesses(REF INT numGuessesInt)
{
串numGuessesString =;
Console.WriteLine(选择了一些猜测);
numGuessesString =到Console.ReadLine();
试
{
numGuessesInt = Convert.ToInt32(numGuessesString);
如果((numGuessesInt<!= 20安培; numGuessesInt> = 1))
{
抛出新的异常();
}
}
赶上(例外)
{
numGuessesInt = -1;
Console.WriteLine(错误:猜测无效号码);
}
}
// PICK WORD
私人静态字符串PickWord()
{
返回字[randomGen。下一个(0,words.Count() - 1)];
}
// ******显示字******
私人静态字符串displayWord(列表<焦炭> guessedCharacters,串字)
{
串returnedWord =;
如果(guessedCharacters.Count == 0)
{
的foreach(字符字字母)
{
returnedWord + =_;
}
返回returnedWord;
}
的foreach(字字符字母)
{
布尔letterMatch = FALSE;
的foreach(在guessedCharacters CHAR字符)
{
如果(字符==字母)
{
returnedWord + =字符+;
letterMatch = TRUE;
中断;
}
,否则
{
letterMatch = FALSE;
}
}
如果(letterMatch ==假)
{
returnedWord + =_;
}
}
返回returnedWord;
}
// ******猜字母******
静态无效guessLetter(列表<焦炭> guessedCharacters ,串词,串wordToDisplay,文献INT numGuessesLeft)
{
包含字母=;
的foreach(在guessedCharacters字符字母)
{
字母+ =+字母;
}
Console.WriteLine(猜一个字母);
Console.WriteLine(孰料快报:+字母);
Console.WriteLine(左猜测:+ numGuessesLeft);
Console.WriteLine(wordToDisplay);
串猜测=到Console.ReadLine();
字符guessedLetter ='A';
试
{
guessedLetter = Convert.ToChar(猜测);
如果(!Char.IsLetter(guessedLetter))
{
抛出新的异常();
}
}
赶上(例外)
{
Console.WriteLine(错误:无效信选择);
// guessLetter(guessedCharacters,文字,wordToDisplay,楼盘numGuessesLeft);
}
布尔重复= FALSE;
的for(int i = 0; I< guessedCharacters.Count;我++)
{
如果(guessedCharacters [I] == guessedLetter)
{
控制台。的WriteLine(错误:无效信选择);
复读= TRUE;
// guessLetter(guessedCharacters,文字,wordToDisplay,楼盘numGuessesLeft);
}
}
如果(重复==假)
{
guessedCharacters.Add(guessedLetter);
numGuessesLeft - = 1;
}
}
// ******检查是否有玩家想再次发挥。 ******
静态无效checkIfPlayAgain()
{
Console.WriteLine(你想再次发挥(Y / N)?);
串playAgain =到Console.ReadLine();
如果(playAgain ==n个)
{
Environment.Exit(1);
}
}
}
Wordlist.cs
使用系统;
使用System.Collections.Generic;
公共类单词列表:列出<串GT;
{
}
I´m having trouble picking a random word from a list in another file. Actually I can´t even get it to choose any word. I´m not sure how to connect the 2 files so to say. Hoping someone can help out, I´m a beginner so please explain as easy as possible:)
I have 2 files, one is called program.cs and the other is called WordList.cs I´m gonna paste all my code but first the little snip that I´m having problem with. I just can´t figure out how to write the code correct.
Here is the little part which is called Pick word:
//PICK WORD
static string pickWord()
{
string returnword = "";
TextReader file = new StreamReader(words);
string fileLine = file.ReadLine();
Random randomGen = new Random();
returnword = words[randomGen.Next(0, words.Count - 1)];
return returnword;
}
And here is all the code in Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class Hangman
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title = "C# Hangman";
Console.WriteLine("Welcome To C# Hangman!");
//MENU
int MenuChoice = 0;
while (MenuChoice != 4)
{
Console.Write("\n\t1) Add words");
Console.Write("\n\t2) Show list of words");
Console.Write("\n\t3) Play");
Console.Write("\n\t4) Quit\n\n");
Console.Write("\n\tChoose 1-4: "); //Choose meny item
MenuChoice = Convert.ToInt32(Console.ReadLine());
WordList showing = new WordList();
switch (MenuChoice)
{
case 1:
Console.Write("\n\tAdd a word\n\n");
var insert = Console.ReadLine();
showing.AddWord(insert);
Console.Write("\n\tList of words\n\n");
showing.ListOfWords();
break;
case 2:
Console.Write("\n\tList of words\n\n");
showing.ListOfWords();
break;
case 3: //Running game
int numGuessesInt = -1;
while (numGuessesInt == -1)
{
/* Sets the number of guesses the user has to guess the word*/
pickNumGuesses(ref numGuessesInt);
}
/* Randomly picks a word*/
string word = pickWord();
/* Creates a list of characters that will show */
List<char> guessedLetters = new List<char>();
bool solved = false;
while (solved == false)
{
/* Displaying a string to the user based on the user's correct guesses.
* If nothing is correct string will return "_ _ _ " */
string wordToDisplay = displayWord(guessedLetters, word);
/* If the string returned contains the "_" character, all the
* correct letters have not been guessed, so checking if user
* has lost, by checking if numGuessesLeft is less than 1.*/
if (!wordToDisplay.Contains("_"))
{
solved = true;
Console.WriteLine("You Win! The word was " + word);
/* Check if the user wants to play again. If they do,
* then solved is set to true, will end the loop,
* otherwise, checkIfPlayAgain will close the program.*/
checkIfPlayAgain();
}
else if (numGuessesInt <= 0)
{
solved = true;
Console.WriteLine("You Lose! The word was " + word);
checkIfPlayAgain();
}
else
{
/* If the user has not won or lost, call guessLetter,
* display the word, minus guesses by 1*/
guessLetter(guessedLetters, word, wordToDisplay, ref numGuessesInt);
}
}
break;
case 4:
Console.WriteLine("\n\tEnd game?\n\n");
break;
default:
Console.WriteLine("Sorry, invalid selection");
break;
}
}
}
// ****** PICK NUMBER OF GUESSES ******
static void pickNumGuesses(ref int numGuessesInt)
{
string numGuessesString = "";
Console.WriteLine("Pick a number of guesses");
numGuessesString = Console.ReadLine();
try
{
numGuessesInt = Convert.ToInt32(numGuessesString);
if (!(numGuessesInt <= 20 & numGuessesInt >= 1))
{
throw new Exception();
}
}
catch (Exception)
{
numGuessesInt = -1;
Console.WriteLine("Error: Invalid Number of Guesses");
}
}
//PICK WORD
static string pickWord()
{
string returnword = "";
TextReader file = new StreamReader(words);
string fileLine = file.ReadLine();
Random randomGen = new Random();
returnword = words[randomGen.Next(0, words.Count - 1)];
return returnword;
}
// ****** Display word ******
static string displayWord(List<char> guessedCharacters, string word)
{
string returnedWord = "";
if (guessedCharacters.Count == 0)
{
foreach (char letter in word)
{
returnedWord += "_ ";
}
return returnedWord;
}
foreach (char letter in word)
{
bool letterMatch = false;
foreach (char character in guessedCharacters)
{
if (character == letter)
{
returnedWord += character + " ";
letterMatch = true;
break;
}
else
{
letterMatch = false;
}
}
if (letterMatch == false)
{
returnedWord += "_ ";
}
}
return returnedWord;
}
// ****** Guess letter ******
static void guessLetter(List<char> guessedCharacters, string word, string wordToDisplay, ref int numGuessesLeft)
{
string letters = "";
foreach (char letter in guessedCharacters)
{
letters += " " + letter;
}
Console.WriteLine("Guess a letter");
Console.WriteLine("Guessed Letters: " + letters);
Console.WriteLine("Guesses Left: " + numGuessesLeft);
Console.WriteLine(wordToDisplay);
string guess = Console.ReadLine();
char guessedLetter = 'a';
try
{
guessedLetter = Convert.ToChar(guess);
if (!Char.IsLetter(guessedLetter))
{
throw new Exception();
}
}
catch (Exception)
{
Console.WriteLine("Error: Invalid Letter Choice");
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
bool repeat = false;
for (int i = 0; i < guessedCharacters.Count; i++)
{
if (guessedCharacters[i] == guessedLetter)
{
Console.WriteLine("Error: Invalid Letter Choice");
repeat = true;
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
}
if (repeat == false)
{
guessedCharacters.Add(guessedLetter);
numGuessesLeft -= 1;
}
}
// ****** Check to see if player wants to play again. ******
static void checkIfPlayAgain()
{
Console.WriteLine("Would you like to play again? (y/n)");
string playAgain = Console.ReadLine();
if (playAgain == "n")
{
Environment.Exit(1);
}
}
}
And here is the code for WordList.cs
using System;
using System.Collections.Generic;
class WordList
{
List <string> words = new List<string>();
public void ListOfWords()
{
words.Add("test"); // Contains: test
words.Add("dog"); // Contains: test, dog
words.Insert(1, "shit"); // Contains: test, shit, dog
words.Sort();
foreach (string word in words) // Display for verification
{
Console.WriteLine(word);
}
}
public void AddWord(string value){
words.Add(value);
}
}
I have made some changes to your code. The code works now but is far from perfect.
Your solution has two files Program.cs
and Wordlist.cs
, which looks like this
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
public class Hangman
{
/*
* Some notes on your code:
* use naming convention for methods and fields, i.e. methods names start with a capital letter
* use modifiers for methods, i.e private, public, protected in your method declarations
* make variables private if you use them on several methods
* and finally: read a book on c#
*
*/
private static WordList words;
private static Random randomGen = new Random();
public static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title = "C# Hangman";
Console.WriteLine("Welcome To C# Hangman!");
initializeWordList();
//MENU
int MenuChoice = 0;
while (MenuChoice != 4)
{
Console.Write("\n\t1) Add words");
Console.Write("\n\t2) Show list of words");
Console.Write("\n\t3) Play");
Console.Write("\n\t4) Quit\n\n");
Console.Write("\n\tChoose 1-4: "); //Choose meny item
MenuChoice = Convert.ToInt32(Console.ReadLine());
switch (MenuChoice)
{
case 1:
Console.Write("\n\tAdd a word\n\n");
var insert = Console.ReadLine();
words.Add(insert);
Console.Write("\n\tList of words\n\n");
foreach (string w in words) // Display for verification
Console.WriteLine(w);
break;
case 2:
Console.Write("\n\tList of words\n\n");
foreach (string w in words) // Display for verification
Console.WriteLine(w);
break;
case 3: //Running game
int numGuessesInt = -1;
while (numGuessesInt == -1)
{
/* Sets the number of guesses the user has to guess the word*/
pickNumGuesses(ref numGuessesInt);
}
/* Randomly picks a word*/
string word = PickWord();
/* Creates a list of characters that will show */
List<char> guessedLetters = new List<char>();
bool solved = false;
while (solved == false)
{
/* Displaying a string to the user based on the user's correct guesses.
* If nothing is correct string will return "_ _ _ " */
string wordToDisplay = displayWord(guessedLetters, word);
/* If the string returned contains the "_" character, all the
* correct letters have not been guessed, so checking if user
* has lost, by checking if numGuessesLeft is less than 1.*/
if (!wordToDisplay.Contains("_"))
{
solved = true;
Console.WriteLine("You Win! The word was " + word);
/* Check if the user wants to play again. If they do,
* then solved is set to true, will end the loop,
* otherwise, checkIfPlayAgain will close the program.*/
checkIfPlayAgain();
}
else if (numGuessesInt <= 0)
{
solved = true;
Console.WriteLine("You Lose! The word was " + word);
checkIfPlayAgain();
}
else
{
/* If the user has not won or lost, call guessLetter,
* display the word, minus guesses by 1*/
guessLetter(guessedLetters, word, wordToDisplay, ref numGuessesInt);
}
}
break;
case 4:
Console.WriteLine("\n\tEnd game?\n\n");
break;
default:
Console.WriteLine("Sorry, invalid selection");
break;
}
}
}
private static void initializeWordList()
{
words = new WordList();
words.Add("test"); // Contains: test
words.Add("dog"); // Contains: test, dog
words.Insert(1, "shit"); // Contains: test, shit, dog
words.Sort();
}
// ****** PICK NUMBER OF GUESSES ******
private static void pickNumGuesses(ref int numGuessesInt)
{
string numGuessesString = "";
Console.WriteLine("Pick a number of guesses");
numGuessesString = Console.ReadLine();
try
{
numGuessesInt = Convert.ToInt32(numGuessesString);
if (!(numGuessesInt <= 20 & numGuessesInt >= 1))
{
throw new Exception();
}
}
catch (Exception)
{
numGuessesInt = -1;
Console.WriteLine("Error: Invalid Number of Guesses");
}
}
//PICK WORD
private static string PickWord()
{
return words[randomGen.Next(0, words.Count() - 1)];
}
// ****** Display word ******
private static string displayWord(List<char> guessedCharacters, string word)
{
string returnedWord = "";
if (guessedCharacters.Count == 0)
{
foreach (char letter in word)
{
returnedWord += "_ ";
}
return returnedWord;
}
foreach (char letter in word)
{
bool letterMatch = false;
foreach (char character in guessedCharacters)
{
if (character == letter)
{
returnedWord += character + " ";
letterMatch = true;
break;
}
else
{
letterMatch = false;
}
}
if (letterMatch == false)
{
returnedWord += "_ ";
}
}
return returnedWord;
}
// ****** Guess letter ******
static void guessLetter(List<char> guessedCharacters, string word, string wordToDisplay, ref int numGuessesLeft)
{
string letters = "";
foreach (char letter in guessedCharacters)
{
letters += " " + letter;
}
Console.WriteLine("Guess a letter");
Console.WriteLine("Guessed Letters: " + letters);
Console.WriteLine("Guesses Left: " + numGuessesLeft);
Console.WriteLine(wordToDisplay);
string guess = Console.ReadLine();
char guessedLetter = 'a';
try
{
guessedLetter = Convert.ToChar(guess);
if (!Char.IsLetter(guessedLetter))
{
throw new Exception();
}
}
catch (Exception)
{
Console.WriteLine("Error: Invalid Letter Choice");
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
bool repeat = false;
for (int i = 0; i < guessedCharacters.Count; i++)
{
if (guessedCharacters[i] == guessedLetter)
{
Console.WriteLine("Error: Invalid Letter Choice");
repeat = true;
//guessLetter(guessedCharacters, word, wordToDisplay, ref numGuessesLeft);
}
}
if (repeat == false)
{
guessedCharacters.Add(guessedLetter);
numGuessesLeft -= 1;
}
}
// ****** Check to see if player wants to play again. ******
static void checkIfPlayAgain()
{
Console.WriteLine("Would you like to play again? (y/n)");
string playAgain = Console.ReadLine();
if (playAgain == "n")
{
Environment.Exit(1);
}
}
}
Wordlist.cs
using System;
using System.Collections.Generic;
public class WordList : List<string>
{
}
这篇关于从列表中选择随机字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!