如何在 C# 中讀取字串形式的輸入?


若想在 C# 中讀取字串形式的輸入,請使用 Console.ReadLine() 方法。

str = Console.ReadLine();

以上示例將讀取字串形式的輸入。我們無需在此使用轉換方法,因為輸入預設採用字串形式。

現在顯示使用者輸入的字串 −

示例

 實際演示

using System;
using System.Collections.Generic;

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

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

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

輸出

Result =

以下為輸出結果。假設使用者輸入了“Amit”作為輸入 −

Result = amit

更新時間: 2020 年 6 月 22 日

7K+ 瀏覽次數

開啟你的 職業生涯

完成課程並獲得認證

開始使用
廣告
© . All rights reserved.