gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:18 25 4
gpt4 key购买 nike

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

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

//}

最佳答案

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

关于C#:方法故障(没有可中断或继续的封闭循环),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22118520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com