簡體   English   中英

為什么我的簡單C#程序立即退出?

[英]Why does my simple C# program quit immediately?

我剛開始學習C#,並且正在解決一個需要讀取名稱列表(由用戶輸入)並再次打印出來的問題。 我最多可以接受20個名字。 如果用戶輸入null或QUIT,則應停止使用名稱。

到目前為止,這是我得到的:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = new string[20]; // create a 20 name array
            int nameCount = 0;
            string userInput;
            Console.WriteLine("Enter a bunch of names!"); // ask for a name
            userInput = Console.ReadLine(); // store the name in userInput
            for (int maxNames = 20; maxNames < names.Length; maxNames++)
            {
                if (userInput == "") // if the entry is null, stop taking names.
                {
                    Console.WriteLine("You entered {0}", names);
                    Console.ReadKey();
                }
                else if (userInput == "QUIT") // if the entry is QUIT, stop taking names.
                {
                    Console.WriteLine("You entered {0}", names);
                    Console.ReadKey();
                }
                else // if it isn't null or QUIT, continue populating the array until we hit the max.
                {
                    names[nameCount] = userInput;
                    nameCount = nameCount + 1;
                    maxNames = maxNames + 1;
                }


            }
        }
    }
}

運行此命令時,我得到“輸入一堆名字!” 提示,但是一旦輸入名稱,控制台就會關閉。 我不確定我在做什么錯。 感謝新手的任何幫助。

更新:謝謝大家的幫助。 我接受了一些不同的建議(以及新的快捷方式!),並得出以下結論:

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = new string[20]; // create a 20 name array
            int nameCount = 0;
            int maxNames = 0;
            Console.WriteLine("Enter a bunch of names!"); // ask for a name
            while (maxNames != 20)
            {
                string userInput = Console.ReadLine(); // store the name in userInput
                if (userInput == "") // if the entry is null, stop taking names.
                {
                    Console.WriteLine("You entered:");
                    foreach (string name in names)
                    {
                        Console.WriteLine(name);
                    }
                    Console.ReadKey();
                }
                else if (userInput == "QUIT") // if the entry is QUIT, stop taking names.
                {
                    Console.WriteLine("You entered:");
                    foreach (string name in names)
                    {
                        Console.WriteLine(name);
                    }
                    Console.ReadKey();
                }
                names[nameCount] = userInput;
                nameCount++;
                maxNames++;
            }
            Console.ReadKey();
        }
    }
}

歡迎來到社區。 似乎有很多人忽略了基本循環。

您將數組聲明為20個名稱...沒問題。

然后,您的for循環STARTING的值為20,該值已經是數組長度的AT。 從0開始循環。

for (int maxNames = 0; maxNames < names.Length; maxNames++)
{
   // Also, move your input WITHIN the loop
   userInput = Console.ReadLine(); // store the name in userInput

   if (userInput == "") // if the entry is null, stop taking names.
   {
      Console.WriteLine("You entered {0}", names);
      Console.ReadKey();
      break;
   }
   else if (userInput == "QUIT") // if the entry is QUIT, stop taking names.
   {
      Console.WriteLine("You entered {0}", names);
      Console.ReadKey();
      break;
   }
   else 
   // if it isn't null or QUIT, continue populating the array until we hit the max.
   {
      // since maxNames is now properly starting at 0,
      // you don't need your other name counter variable.
      // just use maxNames.  The loop process will increase it next cycle through
      names[maxNames] = userInput;
   }
}

該程序立即終止,因為它在獲得第一個輸入后不會進入循環。

static void Main(string[] args)
    {
        List<String> names = new List<string>(); // create a 20 name array
        string userInput;
        int maxNames = 0;
        while (maxNames != 20)
        {
            Console.WriteLine("Enter a bunch of names!"); // ask for a name
            userInput = Console.ReadLine(); // store the name in userInput
            names.Add(userInput);
            maxNames++;
            if(maxNames == 20 || userInput == "" || userInput == "QUIT")
            {
                foreach (string name in names)
                {
                    Console.WriteLine(name);
                }
                Console.ReadKey();
            }
        }
    }

我使用了一個字符串列表來存儲用戶輸入,並在'maxname'為20之后編寫了它。

Console.ReadLine();

在for循環之后結束。

完成后會自動關閉。 通過添加Console.ReadLine(); 最后會指示它保持打開狀態

問題出在您的for循環中:

for (int maxNames = 20; maxNames < names.Length; maxNames++)

您將maxNames初始化為20,然后進行迭代,只要maxNames < names.Length string[] names = new string[20]; 我們知道names.Length20 ,所以在第一次迭代之前條件的計算結果為20 < 20 ,當然是false 因此,永不進入循環,程序退出。 您可能打算將maxNames初始化為0 ,而不是20

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM