[英]Add list item from consol C#
I´m making a very simple Hangman game and using 2 files. 我正在制作一个非常简单的Hangman游戏,并使用2个文件。 Program.cs and WordList.cs.
Program.cs和WordList.cs。
The menu is: 菜单是:
Add words 添加单词
Show list off words 显示字词清单
Play 玩
Exit 出口
I´m wondering how to get a word written in the consol to go into the list of words. 我想知道如何将一个单词写在控制台上,以进入单词列表。 So if I choose menu item 1, I should be able to enter a maximum off 5 words and make them go into the word list.
因此,如果我选择菜单项1,则应该最多可以输入5个单词并将其放入单词列表。 Really hope someone can help as I´ma little lost.
真希望有人能帮忙,因为我有点迷茫。 Need I say beginner at C#:) I haven´t figured out how the program searches each letter yet, but dealing with this problem first...
我需要在C#中说初学者吗?)我还没有弄清楚程序如何搜索每个字母,但是首先要解决这个问题...
Here is the code in program.cs 这是program.cs中的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class Hangman
{
static void Main()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title = "C# Hangman";
Console.WriteLine("Welcome To C# Hangman!");
//MENU
char MenuChoice;
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.ToChar(Console.ReadLine());
switch (MenuChoice)
{
case '1':
break;
case '2':
WordList showing = new WordList();
showing.ListOfWords();
Console.Write("\n\tList of words\n\n");
break;
case '3': //Running game
int guesses;
Console.Write("\n\tHow many faults can you have: ");
guesses = Convert.ToInt32(Console.ReadLine());
Console.Write("\n\tAwesome, let´s play!\n");
String input;
bool wrong;
int NumberOfTries = 0;
do
{
Console.WriteLine("\n\n\tWrong guesses: " + NumberOfTries + " / " + guesses + "\n");
Console.WriteLine("\n\tGuessed letters:\n");
Console.WriteLine("\n\tWord:\n");
Console.Write("\n\n\tGuess letter: ");
input = Console.ReadLine();
Console.Write("\n\n\t ");
wrong = !input.Equals("t") &&
!input.Equals("e") &&
!input.Equals("s") &&
!input.Equals("t");
if (wrong)
{
NumberOfTries++;
Console.WriteLine("\n\tWrong letter " + "Try again!");
}
if (wrong && (NumberOfTries > guesses - 1))
{
Console.WriteLine("\n\tYou have failed " + guesses + ". End of game!\n");
break;
}
} while (wrong);
if (!wrong)
Console.WriteLine("\n\tWhohoo!");
break;
case '4':
Console.WriteLine("\n\tEnd game?\n\n");
break;
}
}
}
And here is the code in WordList.cs 这是WordList.cs中的代码
using System;
using System.Collections.Generic;
class WordList
{
public void ListOfWords()
{
List<string> words = new List<string>(); // List
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);
}
}
}
You could use Console.ReadLine()
您可以使用
Console.ReadLine()
string word = "";
while (word != null && !word.Equals("DONE"))
{
word = Console.ReadLine();
wordList.Add(word);
}
Extend your app like this with moving your showing declaration outside of your switch 通过将显示声明移到开关之外来扩展您的应用程序
var showing = new WordList();
switch (MenuChoice)
{
case '1':
showing.AddWord(Console.ReadLine())
break;
case '2':
showing = new WordList();
showing.ListOfWords();
Console.Write("\n\tList of words\n\n");
And extend your Wordlist to keep your words and add a method to add new words 并扩展您的单词表以保留您的单词,并添加一种方法来添加新单词
class WordList
{
private words = new List<string>();
'keep the constructor but move declaration
public void AddWord(string word)
{
words.Add(word);
}
In fact with some refactoring you can keep remove the class wordlist and just keep the list in your Program.cs, but it really could use more then that as refactoring 实际上,通过进行一些重构,您可以继续删除类单词列表,并将其保留在Program.cs中,但实际上它可以使用更多的单词作为重构
I'll try and modify your code in total (don't got a compiler right now so don't blame any minor syntax issues (using VB.net normally) 我将尝试总计修改您的代码(现在没有编译器,所以不要怪任何小语法问题(通常使用VB.net)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class Hangman
{
static void Main()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Title = "C# Hangman";
Console.WriteLine("Welcome To C# Hangman!");
//MENU
char MenuChoice;
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.ToChar(Console.ReadLine());
WordList showing = new WordList();
switch (MenuChoice)
{
case '1':
var input = Console.ReadLine();
showing.AddWord(input);
break;
case '2':
showing.ListOfWords();
Console.Write("\n\tList of words\n\n");
break;
case '3': //Running game
int guesses;
Console.Write("\n\tHow many faults can you have: ");
guesses = Convert.ToInt32(Console.ReadLine());
Console.Write("\n\tAwesome, let´s play!\n");
String input;
bool wrong;
int NumberOfTries = 0;
do
{
Console.WriteLine("\n\n\tWrong guesses: " + NumberOfTries + " / " + guesses + "\n");
Console.WriteLine("\n\tGuessed letters:\n");
Console.WriteLine("\n\tWord:\n");
Console.Write("\n\n\tGuess letter: ");
input = Console.ReadLine();
Console.Write("\n\n\t ");
wrong = !input.Equals("t") &&
!input.Equals("e") &&
!input.Equals("s") &&
!input.Equals("t");
if (wrong)
{
NumberOfTries++;
Console.WriteLine("\n\tWrong letter " + "Try again!");
}
if (wrong && (NumberOfTries > guesses - 1))
{
Console.WriteLine("\n\tYou have failed " + guesses + ". End of game!\n");
break;
}
} while (wrong);
if (!wrong)
Console.WriteLine("\n\tWhohoo!");
break;
case '4':
Console.WriteLine("\n\tEnd game?\n\n");
break;
}
}
}
And here is the code in WordList.cs 这是WordList.cs中的代码
using System;
using System.Collections.Generic;
class WordList
{
private 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);
}
}
You've got a collection of words. 你有很多话。
List<string> myList = new List<string>();
You read from the console the word 您从控制台读取单词
var inputString = Console.ReadLine();
You could trim it if you like 如果愿意,可以修剪一下
inputString = inputString.Trim(); // that'll remove the spaces on the front/back of the string
Then you can add it to the list 然后可以将其添加到列表中
myList.Add(inputString);
Since words are separated by spaces, you can have the user enter their list of words like this: 由于单词之间用空格隔开,因此您可以让用户输入其单词列表,如下所示:
these are four words
, which you can read easily with these are four words
,您可以轻松阅读
string input = Console.ReadLine();
// input == "these are four words"
Creating the list is now very simple 现在创建列表非常简单
string[] words1 = input.Split(new char[] { ' ' }, // Splits the words by space
StringSplitOptions.RemoveEmptyEntries);
// words1 = { "these", "are", "four", "words" }
If you absolutely need a List<string>
, you just need to add .ToList()
at the end: 如果您绝对需要
List<string>
,则只需在末尾添加.ToList()
:
List<string> words2 = input.Split(new char[] { ' ' },
StringSplitOptions.RemoveEmptyEntries).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.