嵌套if和for循环问题

时间:2018-12-25 03:19:08

标签: c#

我和if和for循环有问题。第一个结束括号(})关闭第一个if语句。我想要做的是关闭与之对齐的支架。

startnum = int.Parse(startnumbers);

endnum = int.Parse(endnumbers);

string route = "1. ";

if (startletters == endletters && startnum > endnum)
{
   for (int count = 0; startnum < endnum; startnum++)
   {
      if (startname != endname)
      {
         count++;
         route += ("Board the {0} line, go {1} stops toward {2}",startletters,count, endname );
      }
   }
}

This is the screenshot

2 个答案:

答案 0 :(得分:3)

您在这行中有语法错误:

route += ("Board the {0} line, go {1} stops toward {2}",startletters,count, endname );

您具有string.Format的参数,但实际上并未调用它。您要改为:

route += string.Format("Board the {0} line, go {1} stops toward {2}", startletters, count, endnum);

由于语法错误,编译器无法按预期匹配花括号。

答案 1 :(得分:1)

您将要犯的以下错误:

enter image description here

如何解决?

以下解决此问题的方法是: enter image description here

您的代码[已更正]:

startnum = int.Parse(startnumbers);

endnum = int.Parse(endnumbers);

string route = "1.";

if (startletters == endletters && startnum > endnum)
{
   for (int count = 0; startnum < endnum; startnum++)
   {
      if (startname != endname)
      {
         count++;
         string tempRoute = string.Format("Board the {0} line, go {1} stops toward {2}",startletters,count, endname);
         route = route + " " + tempRoute;
      }
   }
}