问题解决了!感谢你们!!!
这是一个关于 C# 的问题。
我有一个包含 4 个选项的菜单:
- 添加单词
- 显示单词列表
- 玩
- 退出
我用一个开关做到了,开关在一个while循环内,以便在执行任何选择后能够返回到meny。
我遇到的问题是,当我从菜单中进行选择时,什么都没有发生,我收到一条错误消息,表明程序已停止。它没有循环……在我把它放在 while 之前它工作正常环形。因此,while 循环中的开关无法正常工作。
非常感谢提供的任何帮助。我是初学者,所以请尝试简单地解释一下:)
我正在粘贴我到目前为止位于 2 个文件中的所有代码。一个叫做program.cs,另一个叫做wordlist.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
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();
MenuChoice = int.Parse(Console.ReadLine());
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 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 congrats!");
break;
case '4':
Console.WriteLine("\n\tEnd game?\n\n");
break;
default:
Console.WriteLine("Sorry, invalid selection");
break;
}
MenuChoice++;
if (MenuChoice < 30)
continue;
else
break;
}
}
}
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);
}
}