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

时间:2014-03-01 18:32:09

标签: c# methods

Newb在这里努力成为一个业余爱好者。

我当前的项目使用方法,类,列表来显示书籍和评论,并允许用户在控制台视图中输入自己的评论。我已经建立了我的课程,他们现在正在工作,所以我暂时将他们删除,尽可能清楚地揭露我当前的问题。得到我的问题,我觉得我的程序很快变得很快,所以将一些代码移动到我称之为“选择”的方法似乎是个好主意。在我将代码移动到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");

    //}

7 个答案:

答案 0 :(得分:4)

嗯,你不是一个循环,所以要终止方法的执行,关键字是'return'

答案 1 :(得分:2)

重构代码后,它不再知道它位于while循环内,因此break关键字是不必要的。

您使用break来终止程序,因此您需要执行其他操作才能退出while循环。您可以通过多种方式重构,但您可以尝试:

while (true)
{
    var command
      = GetString(" \n\n(V)eiw, (S)elect, (C)omment, (R)emove, (E)xit").ToLower();

    if (command == "e")
        break;

    Select(command);

    Console.Read();
}

command传递给Select(),因此您的大多数逻辑仍在另一种方法中......只是不要在command == "e"中测试Select()

答案 2 :(得分:1)

只需省略break;语句,因为不需要它,只有当您想要退出执行循环时才需要它。

答案 3 :(得分:1)

问题:您无法与if block分开,因为它未包含在任何loop中。

解决方案:您只需从if block返回条件的状态,以确保循环从调用方函数中断。

1。如果命令等于“e”则返回true,以便调用者断开循环。
2。 else返回false,以便循环继续。

 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
         * */
         if(Select())
             break;   //break if you get return value of Select() as true
         Console.Read();
     }
}


 public bool 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
         return false;
        }

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

        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"); }                

             return false;           
           }

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

答案 4 :(得分:1)

中断关键字适用于循环或切换块。在Select方法中没有这样的语句。您可以从选择方法返回 true / false ,在播放方法中,您可以根据返回值调用 break ;

if(Select())
{
     break;
}

答案 5 :(得分:1)

我会更新您的Select方法以返回一个布尔值,表明输入有效。然后,您可以按如下方式设置while条件:

while (Select())
{
    /*
     * 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
     * */     
    Console.Read();
}

如果你有这样的话,我也会阻止使用其他许多东西,并将它们更新为使用switch / case。对于案例s,您也可以将其转换为单独的方法。

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

    switch (command)
    {
        case "v":
            Announcer(" \n\nEnter listing: ");
        //ViewDirectory();//call view directory here
            break;
        case "c":
            Announcer(" \n\nEnter comment: ");
            break;
        case "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()

            switch (select)
            {
                case "1":
                    Announcer(" \n\nDisplay book info + allow for user comment entering"); 
                    break;
                case "2":
                    Announcer(" \n\nDisplay book info + allow for user comment entering");
                    break;
                case "3":
                    Announcer(" \n\nDisplay book info + allow for user comment entering");
                    break;
            }
            break;
        }
        case "e":
            isValid = false;
            break;
        default:
            Console.WriteLine("\n\nOopsy, I don't know that command! \n\n");
            break;
    }
    return isValid;
}

答案 6 :(得分:1)

如果你有一个switch语句,不要在case语句之外放置任何东西,否则你会得到那个错误。

switch (val)
{
    //this line should not be here
    dosomething1(); //==>error
    case "low":
        dosomething2();
        break;
    case "high":
        dosomething3();
        break;
}