for循环在出现中断时保持循环

时间:2020-10-08 13:22:24

标签: c# .net visual-studio

我正在尝试制定一个停车场程序,其中一项功能是移动车辆。它正在工作,对象正在更改数组中的位置,只是循环继续进行并且崩溃,因为parkingLot [i,j] .RegNummer第二次循环通过时为null。当我“移动”汽车后,如何阻止它再次循环?我希望它打破并返回菜单。 (这是在连接到菜单的开关盒中的)

Console.WriteLine("Enter the licensenumber of the vehicle that you want to move: ");
                        string lNumber = Console.ReadLine();
                        for (int i = 0; i < 100; i++)
                        {
                            for (int j = 0; j < 2; j++)
                            {
                                if (lNumber == parkingLot[i, j].RegNummer)
                                {
                                    Console.WriteLine("Enter the space you want to move it to: ");
                                    int space = int.Parse(Console.ReadLine());
                                    int space1 = space - 1;
                                    if (parkingLot[space1, 0] == null)
                                    {
                                        parkingLot[space1, 0] = parkingLot[i, j];
                                        parkingLot[i, j] = null;
                                        Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                                        break;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine("The vehicle with licensenumber {0} could not be found!", lNumber);
                                    break;
                                }
                            }
                        }

2 个答案:

答案 0 :(得分:2)

您的break语句退出内部for循环,如果您需要退出外部for循环,我建议使用boolean,并检查它是否true退出外循环。当您在内部for循环中满足条件时,请将其设置为true

 string lNumber = Console.ReadLine();
 bool exitOuter = false; //Use boolean to exit outer for loop
 for (int i = 0; i < 100; i++)
 {        
     for (int j = 0; j < 2; j++)
     {
         if (lNumber == parkingLot[i, j].RegNummer)
         {
             Console.WriteLine("Enter the space you want to move it to: ");
             int space = int.Parse(Console.ReadLine());
             int space1 = space - 1;
             if (parkingLot[space1, 0] == null)
             {
                 parkingLot[space1, 0] = parkingLot[i, j];
                 parkingLot[i, j] = null;
                 Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                 exitOuter = true; //Set boolean to true to exit outer for loop
                 break;
              }
         }
         else
         {
              Console.WriteLine("The vehicle with licensenumber {0} could not be found!", lNumber);
              break;
         }
      }
      
      //Check boolean to break outer for loop
      if(exitOuter)
         break;
                
 }

答案 1 :(得分:1)

因此,问题在于您只是从内循环中中断。

                                   if (parkingLot[space1, 0] == null)
                                {
                                    parkingLot[space1, 0] = parkingLot[i, j];
                                    parkingLot[i, j] = null;
                                    Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                                    break;
                                }

您必须告知外圈汽车已停放,并且必须结束任务。 您可以为此实现一个标志。

        Console.WriteLine("Enter the licensenumber of the vehicle that you want to move: ");
        string lNumber = Console.ReadLine();
        bool carParked = false;
        for (int i = 0; i < 100; i++)
        {
            for (int j = 0; j < 2; j++)
            {
                if (lNumber == parkingLot[i, j].RegNummer)
                {
                    Console.WriteLine("Enter the space you want to move it to: ");
                    int space = int.Parse(Console.ReadLine());
                    int space1 = space - 1;
                    if (parkingLot[space1, 0] == null)
                    {
                        parkingLot[space1, 0] = parkingLot[i, j];
                        parkingLot[i, j] = null;
                        Console.WriteLine("The vehicle with licensenumber {0} has been moved to space {1}", lNumber, space);
                        carParked = true;
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("The vehicle with licensenumber {0} could not be found!", lNumber);
                    break;
                }
            }
            if (carParked)
            {
                break;
            }
        }