如何在 C# 中從控制檯讀取一行?


ReadLine() 方法用於在 C# 中從控制檯讀取一行。

str = Console.ReadLine();

上述程式碼會將行設定在變數 str 中。

示例

 實際演示

using System;
using System.Collections.Generic;

class Demo {
   static void Main() {
      string str;

      // use ReadLine() to read the entered line
      str = Console.ReadLine();

      // display the line
      Console.WriteLine("Input = {0}", str);
   }
}

輸出

Input =

以上內容中,我們使用 Console.ReadLine() 方法顯示了一行。字串由使用者從命令列輸入。

更新於: 22-6 月-2020

2K+ 瀏覽量

開啟你的事業

完成課程即可獲得認證

入門
廣告
© . All rights reserved.