问题解决了!感谢大伙们!!!

这是有关C#的问题。

我有一个包含4个选项的菜单:


添加单词
显示单词列表

退出


我用一个开关完成了该开关,该开关位于while循环内,以便能够在执行任何选择后返回到正常状态。

我遇到的问题是,当我从菜单中进行选择时什么也没有发生,并且收到一条错误消息,表明程序已停止。而且它没有循环播放...
在将它放入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);
      }
}

最佳答案

您要两次设置条件:

// This is the one you likely want, as it will read "1", "2", "3", "4", etc
MenuChoice = Convert.ToInt32(Console.ReadLine());
WordList showing = new WordList();

// Remove this!  You only need to read once...
// MenuChoice = int.Parse(Console.ReadLine());


另外,您的case语句将打开字符,而不是数字:

case 1: // Change to 1, not '1'


由于您要转换为上述int值,因此需要打开数值。

关于c# - 不在循环内执行switch语句,并且循环不循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16108634/

10-13 08:34
查看更多