打破 while 循环并且不打印结果 c#

breaking while loop and doesn't print the result c#

我是 c# 的初学者,我正在接受 codeforces 和 SPOJ 的培训,以学习如何解决问题, 我的问题是当循环中断时不打印所有输入它只是结束程序而不打印

有人能告诉我我写的这两个代码有什么错误>

输入: 1,2,88,42 ,99 输出: 1,2,88

class Program

 {

  static void Main(string[] args)
    {
        Console.WriteLine("please enter an integer number in two digits");

        int numbers;
        int[] array_num = new int[100];

        string nu = "";
        int i =1;
      
         while(i<100)
         {
             numbers = int.Parse(Console.ReadLine());
             nu += numbers;
             array_num[i] = numbers;
             

            if(array_num[i]<array_num[i-1])

            {
                Console.WriteLine("the numbers is " +nu+ "");
                break;
            }

            i++;
            
           
         }
       
       }

      }

我在 for 循环中执行此操作,同样的问题

   static void Main(string[] args)
    {
        Console.WriteLine("please enter an integer number in two digits");

        int numbers;
        int[] array_num = new int[50];

        string nu = "";

        for (int i = 1; i <= array_num.Length; i++)
        {
            numbers = int.Parse(Console.ReadLine());
            nu += numbers;
            array_num[i] = numbers;
           
            if (array_num[i] < array_num[i - 1])
            {

                Console.WriteLine("the number is " + nu + "");
                           
                break;
            }
           
        }

    }

哪里出错了?

回答 "...问题是当循环中断时不会打印所有输入它只是结束程序...":

那是因为在你中断循环后没有更多的代码可以执行 - 所以控制台关闭。您可以在 Main 方法的末尾添加 Console.ReadKey()Console.ReadLine() 以在循环中断后保持控制台打开。 Console.ReadKey() 会等到您按任意键,Console.ReadLine() 会等到您输入内容并按 Enter(或直接按 Enter)。或者以这两种方式,控制台将保持打开状态,直到您手动关闭它。

static void Main(string[] args)
{
    // ...
    for (int i = 1; i <= array_num.Length; i++)
    {
        // ...
        if (array_num[i] < array_num[i - 1])
        {
            Console.WriteLine("the number is " + nu + "");
            break;
        }
    }
    
    Console.ReadKey(); // Here Console will wait until you press any key and will stay opened
}