C# 未在循环内执行的Switch语句和未循环的循环

C# 未在循环内执行的Switch语句和未循环的循环,c#,while-loop,switch-statement,C#,While Loop,Switch Statement,问题解决了!谢谢大家 这里有一个关于C的问题 我有一个包含4个选项的菜单: 加词 显示单词列表 玩 退出 我用一个开关做了它,开关在while循环中,以便在执行任何选择后能够返回meny 我遇到的问题是,当我从菜单中进行选择时,什么也没有发生,我收到一条错误消息,程序停止了。。而且它不是循环的。。。 在我把它放入while循环之前,它工作得很好。因此while循环中的开关不起作用 非常感谢您给予的帮助。我是初学者,请尽量简单解释:) 我正在粘贴我所有的代码,到目前为止我已经在2个文件中找到了这些

问题解决了!谢谢大家

这里有一个关于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;
            }
    
        }
    
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用System.Linq;
    使用系统文本;
    使用System.IO;
    阶级刽子手
    {
    静态void Main()
    {
    Console.ForegroundColor=ConsoleColor.Red;
    Console.Title=“C#Hangman”;
    Console.WriteLine(“欢迎来到C#Hangman!”);
    //菜单
    int MenuChoice=0;
    while(MenuChoice!=4)
    {
    Console.Write(“\n\t1)添加单词”);
    Console.Write(“\n\t2)显示单词列表”);
    Console.Write(“\n\t3)Play”);
    控制台。写入(“\n\t4)退出\n\n”);
    Console.Write(“\n\t选择1-4:”);//选择菜单项
    MenuChoice=Convert.ToInt32(Console.ReadLine());
    单词列表显示=新单词列表();
    MenuChoice=int.Parse(Console.ReadLine());
    开关(MenuChoice)
    {
    案例“1”:
    Console.Write(“\n\t输入一个单词\n\n”);
    var insert=Console.ReadLine();
    显示.AddWord(插入);
    Console.Write(“\n\t单词列表\n\n”);
    showing.ListOfWords();
    打破
    案例“2”:
    Console.Write(“\n\t单词列表\n\n”);
    showing.ListOfWords();
    打破
    案例“3”://运行游戏
    智力猜测;
    Console.Write(“\n\t您可能有多少故障:”);
    猜测=Convert.ToInt32(Console.ReadLine());
    Console.Write(“\n\t我们来玩吧!\n”);
    字符串输入;
    布尔错了;
    int numberofthries=0;
    做
    {
    Console.WriteLine(“\n\n\t错误猜测:“+NumberOfTries+”/“+guesses+”\n”);
    Console.WriteLine(“\n\t使用的字母:\n”);
    Console.WriteLine(“\n\tWord:\n”);
    控制台。写(“\n\n\t错误字母:”);
    input=Console.ReadLine();
    Console.Write(“\n\n\t”);
    错误=!input.Equals(“t”)&&
    !input.Equals(“e”)&&
    !input.Equals(“s”)&&
    !input.Equals(“t”);
    如果(错误)
    {
    numberofthries++;
    Console.WriteLine(“\n\t小写字母“+”再试一次!”);
    }
    如果(错误&(次数>猜测-1))
    {
    Console.WriteLine(“\n\t您失败了”+猜测+”。游戏结束!\n”);
    打破
    }
    } 
    而(错),;
    如果(!错)
    Console.WriteLine(“\n\t恭喜你!”);
    打破
    案例“4”:
    Console.WriteLine(“\n\t游戏?\n\n”);
    打破
    违约:
    Console.WriteLine(“对不起,选择无效”);
    打破
    }
    MenuChoice++;
    如果(MenuChoice<30)
    继续;
    其他的
    打破
    }
    }
    }
    
    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);
          }
    }
    
    使用系统;
    使用System.Collections.Generic;
    类词表
    {
    列表单词=新列表();
    公开作废词汇表()
    {
    words.Add(“test”);//包含:test
    words.Add(“dog”);//包含:test,dog
    words.Insert(1,“shit”);//包含:test,shit,dog
    words.Sort();
    foreach(单词中的字符串)//显示以供验证
    {
    控制台写入线(word);
    }
    }
    公共void AddWord(字符串值){
    增加(价值);
    }
    }
    
    在开关循环中,您得到的是MenuChoice,它是整数。但在某些情况下,您会将其与Char进行比较。所以,这就是异常的来源。改变

    case '1':    
    

    还有其他

    您在这里还调用了Console.Readline()两次:

     MenuChoice = Convert.ToInt32(Console.ReadLine());
     WordList showing = new WordList();
    
     MenuChoice = int.Parse(Console.ReadLine());
    

    您可以删除最后一个,因为在我看来它是无用的。

    在您的切换循环中,您得到的是MenuChoice,它是整数。但在某些情况下,您会将其与Char进行比较。所以,这就是异常的来源。改变

    case '1':    
    

    还有其他

    您在这里还调用了Console.Readline()两次:

     MenuChoice = Convert.ToInt32(Console.ReadLine());
     WordList showing = new WordList();
    
     MenuChoice = int.Parse(Console.ReadLine());
    

    您可以删除最后一个,因为我认为它没有用。

    您设置了两次条件:

    // 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'    
    

    由于要转换为上面的整数,因此需要打开数值。

    您要设置两次条件:

    // 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'    
    

    由于要转换为上面的整数,因此需要打开数值。

    考虑以下代码行:

    MenuChoice = int.Parse(Console.ReadLine());
    switch (MenuChoice)
    {
        case '1':
    
    您得到一个
    int
    ,然后与
    char
    进行比较。使用
    案例1:

    另外,为什么是b