C#:方法故障(没有可中断或继续的封闭循环)

Che*_*ire 1 c# methods

新手在这里努力成为一名业余爱好者。

我当前的项目使用方法、类、列表来显示书籍和评论,并允许用户在控制台视图中输入自己的评论。我已经构建了我的类,并且它们此时正在工作,因此我暂时删除了它们,以尽可能清楚地暴露我当前的问题。解决我的问题是,我觉得我的程序很快就变得很大,所以将一些代码移到我称为“选择”的方法中似乎是个好主意。在我将代码移至 select 方法之前,它工作正常/符合预期。但现在我在测试时遇到错误:没有可以打破的封闭循环

具体错误发生在线else if (command == "e"){break;}路上

我尝试将关键字“break”替换为“Continue”,但这不起作用。我已经浏览了网络和 stackoverflow,但没有找到任何我可以理解的内容,以我的理解水平来解决(我仍然是一个新手)。

代码:

    class Program
{
    public void Play(){
        Announcer(" \n\nProgram Name Goes Here \n\n");

        while (true)
        {
            /*
             * Allow user to display director of books (Three)
             * allow user to select specific book with any comments it might have (2-4 comments)
             * Allow user to enter a specific comment
             * display book with new new added comment
             * Allow user to exit book
             * */
            Select();                
        Console.Read();
        }
    }


    public void Announcer(String strTxt){Console.Write(strTxt);}

    public String GetString(String strData){
        Console.WriteLine(strData);
        return Console.ReadLine();//traffic control => back to program
    }

    public void Select(){
        String command = GetString(" \n\n(V)eiw, (S)elect, (C)omment, (R)emove, (E)xit").ToLower();

            if (command == "v")
            { Announcer(" \n\nEnter listing: ");
              //ViewDirectory();//call view directory here
            }

            else if (command == "c")
            { Announcer(" \n\nEnter comment: "); }

            else if (command == "s")
            {                   
                //we want to do a selectString method that returns length of selects here
                String select = GetString(" \n\n(1)st Selection, (2)nd Selection, (3)rd book, (E)xit").ToLower();
                if (select == "1")
                { Announcer(" \n\nDisplay book info + allow for user comment entering"); }

                else if (select == "2")
                { Announcer(" \n\nDisplay book info + allow for user comment entering"); }

                else if (select == "3")
                { Announcer(" \n\nDisplay book info + allow for user comment entering"); }                
            }

            else if (command == "e") { break; }
            else { Console.WriteLine("\n\nOopsy, I don't know that command! \n\n"); }
        }
    }

    //public void ViewDirectory(){
    //    Console.WriteLine("stuff");

    //}
Run Code Online (Sandbox Code Playgroud)

Rot*_*tem 5

好吧,你不在循环中,所以要终止方法的执行,关键字是“return”