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


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

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()方法展示了一行內容。字串由使用者透過命令列輸入。

更新日期: 2020年6月22日

超過2千次檢視

啟動你的 職業生涯

完成課程即可獲得認證

開始
廣告