控制台C#调用嵌套函数
嘿,伙计们,你们能告诉我怎么称呼“打印:如果。它永远不会进入打印如果。它会循环控制台C#调用嵌套函数,c#,loops,console,C#,Loops,Console,嘿,伙计们,你们能告诉我怎么称呼“打印:如果。它永远不会进入打印如果。它会循环 static void Main(string[] args) { if (commands == "Read" || commands == "read") { fileread obj = new fileread(); lcsString = obj.getlcs(); commands = Conso
static void Main(string[] args)
{
if (commands == "Read" || commands == "read")
{
fileread obj = new fileread();
lcsString = obj.getlcs();
commands = Console.ReadLine(); // If command = print I want it go to print but it never goes . it loops out
}
else if (commands =="print")
{
}
}
你可以用while,给你
while (!commands.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
if (commands.Equals("read", StringComparison.OrdinalIgnoreCase))
{
fileread obj = new fileread();
lcsString = obj.getlcs();
}
else if (commands == "print")
{
// print ...
}
commands = Console.ReadLine();
}
不清楚你在问什么,但似乎有一个选择 在if语句中将
commands=Console.ReadLine()移动到外部
commands = Console.ReadLine();
if (commands == "Read" || commands == "read")
{
fileread obj = new fileread();
lcsString = obj.getlcs();
}
else if (commands =="print")
{
}
因为如果你的第一个if
语句起作用,那意味着你的命令是Read
或Read
。在那之后,你的程序就不会进入你的else if
部分。它超出了你的if语句的范围。我在这里没有看到任何循环。也不清楚你的要求是什么。我的意思是它完成了执行希望程序再次运行。在不区分大小写的情况下比较字符串的最佳方法是:if(commands.ToLower()=“read”)。但是你应该编辑你的帖子,更好地解释你的问题,这会让人困惑。“commands”的值“已更新,因此如果需要,我如何调用打印。这是我的问题。因为我已经知道如果条件匹配,if不能继续到另一个。一旦你的代码进入if块,它就不能转到else块谢谢。我只是想了想!对不起:(给你们添麻烦了。