<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # C# 基本輸入和輸出 > 原文: [https://www.programiz.com/csharp-programming/basic-input-output](https://www.programiz.com/csharp-programming/basic-input-output) #### 在本教程中,我們將學習如何使用各種方法從用戶那里獲取輸入并在 C# 中顯示輸出 ## C# 輸出 為了在 C# 中輸出內容,我們可以使用 ```cs System.Console.WriteLine() OR System.Console.Write() ``` 此處,`System`是[命名空間](https://www.programiz.com/csharp-programming/namespaces "C# namespaces"),`Console`是命名空間`System`中的類,`WriteLine`和`Write`是類`Console`的方法。 讓我們看一個簡單的示例,該示例將字符串輸出到輸出屏幕。 ### 示例 1:使用`WriteLine()`打印字符串 ```cs using System; namespace Sample { class Test { public static void Main(string[] args) { Console.WriteLine("C# is cool"); } } } ``` 當我們運行程序時,輸出將是 ```cs C# is cool ``` * * * ### `WriteLine()`和`Write()`方法之間的區別 `WriteLine()`和`Write()`之間的主要區別在于`Write()`方法僅打印提供給它的字符串,而`WriteLine()`方法則打印字符串并移至下一行的開頭。 讓我們看下面的示例,以了解這些方法之間的區別。 #### 示例 2:如何使用`WriteLine()`和`Write()`方法? ```cs using System; namespace Sample { class Test { public static void Main(string[] args) { Console.WriteLine("Prints on "); Console.WriteLine("New line"); Console.Write("Prints on "); Console.Write("Same line"); } } } ``` 當我們運行程序時,輸出將是: ```cs Prints on New line Prints on Same line ``` * * * ### 使用`WriteLine()`和`Write()`打印變量和字面值 `WriteLine()`和`Write()`方法可用于打印變量和字面值。 這是一個例子。 #### 示例 3:打印變量和字面值 ```cs using System; namespace Sample { class Test { public static void Main(string[] args) { int value = 10; // Variable Console.WriteLine(value); // Literal Console.WriteLine(50.05); } } } ``` 當我們運行程序時,輸出將是: ```cs 10 50.05 ``` * * * ### 使用`+`運算符組合(連接)兩個字符串并打印它們 在打印時,可以使用`+`操作符對字符串進行合并/連接。 #### 示例 4:打印使用`+`運算符連接的字符串 ```cs using System; namespace Sample { class Test { public static void Main(string[] args) { int val = 55; Console.WriteLine("Hello " + "World"); Console.WriteLine("Value = " + val); } } } ``` 當我們運行程序時,輸出將是: ```cs Hello World Value = 55 ``` * * * ### 使用字符串格式化打印連接的字符串【更好的選擇】 打印連接字符串的更好的選擇是使用格式化字符串。 格式化字符串允許程序員使用占位符作為變量。 例如, 下一行, ```cs Console.WriteLine("Value = " + val); ``` 可以替換為 ```cs Console.WriteLine("Value = {0}", val); ``` `{0}`是變量`val`的占位符,它將由`val`的值替換。 由于僅使用一個變量,因此只有一個占位符。 格式化的字符串中可以使用多個變量。 我們將在下面的示例中看到這一點。 #### 示例 5:使用字符串格式化打印連接的字符串 ```cs using System; namespace Sample { class Test { public static void Main(string[] args) { int firstNumber = 5, secondNumber = 10, result; result = firstNumber + secondNumber; Console.WriteLine("{0} + {1} = {2}", firstNumber, secondNumber, result); } } } ``` 當我們運行程序時,輸出將是: ```cs 5 + 10 = 15 ``` 在此,將`{0}`替換為`firstNumber`,將`{1}`替換為`secondNumber`,將`{2}`替換為`result`。 與使用`+`運算符相比,這種打印輸出的方法更具可讀性,并且不易出錯。 要了解有關字符串格式的更多信息,請訪問 *C# 字符串格式*。 * * * ## C# 輸入 在 C# 中,從用戶獲取輸入的最簡單方法是使用`Console`類的`ReadLine()`方法。 但是,`Read()`和`ReadKey()`也可用于從用戶那里獲取輸入。 它們也包含在`Console`類中。 ### 示例 6:從用戶獲取字符串輸入 ```cs using System; namespace Sample { class Test { public static void Main(string[] args) { string testString; Console.Write("Enter a string - "); testString = Console.ReadLine(); Console.WriteLine("You entered '{0}'", testString); } } } ``` 當我們運行程序時,輸出將是: ```cs Enter a string - Hello World You entered 'Hello World' ``` * * * ### `ReadLine()`,`Read()`和`ReadKey()`方法之間的區別: `ReadLine()`,`Read()`和`ReadKey()`方法之間的區別是: * `ReadLine()`:`ReadLine()`方法從標準輸入流讀取下一行輸入。 它返回相同的字符串。 * `Read()`:`Read()`方法從標準輸入流中讀取下一個字符。 它返回字符的 ascii 值。 * `ReadKey()`:`ReadKey()`方法獲取用戶按下的下一個鍵。 此方法通常用于按住屏幕,直到用戶按下某個鍵為止。 如果您想了解更多有關這些方法的信息,請參見以下關于 StackOverflow 的有趣討論: [`Console.Read()`和`Console.ReadLine()`之間的區別?](https://stackoverflow.com/questions/6825943/difference-between-console-read-and-console-readline "Difference between Read and ReadLine") 。 * * * #### 示例 7:`Read()`和`ReadKey()`方法之間的區別 ```cs using System; namespace Sample { class Test { public static void Main(string[] args) { int userInput; Console.WriteLine("Press any key to continue..."); Console.ReadKey(); Console.WriteLine(); Console.Write("Input using Read() - "); userInput = Console.Read(); Console.WriteLine("Ascii Value = {0}",userInput); } } } ``` 當我們運行程序時,輸出將是: ```cs Press any key to continue... x Input using Read() - Learning C# Ascii Value = 76 ``` 從此示例中,必須清楚`ReadKey()`和`Read()`方法的工作方式。 在使用`ReadKey()`時,一旦按下該鍵,它就會顯示在屏幕上。 使用`Read()`時,它占一行,但僅返回第一個字符的 ASCII 值。 因此,將打印`76`(`L`的 ASCII 值)。 * * * ### 讀取數值(整數和浮點類型) 在 C# 中,讀取字符或字符串非常簡單。 您需要做的就是根據需要調用相應的方法。 但是,在 C# 中讀取數字值可能會有些棘手。 我們仍將使用與獲取字符串值相同的`ReadLine()`方法。 但是由于`ReadLine()`方法將輸入作為字符串接收,因此需要將其轉換為整數或浮點類型。 轉換輸入的一種簡單方法是使用`Convert`類的方法。 #### 示例 8:使用`Convert`類從用戶讀取數值 ```cs using System; namespace UserInput { class MyClass { public static void Main(string[] args) { string userInput; int intVal; double doubleVal; Console.Write("Enter integer value: "); userInput = Console.ReadLine(); /* Converts to integer type */ intVal = Convert.ToInt32(userInput); Console.WriteLine("You entered {0}",intVal); Console.Write("Enter double value: "); userInput = Console.ReadLine(); /* Converts to double type */ doubleVal = Convert.ToDouble(userInput); Console.WriteLine("You entered {0}",doubleVal); } } } ``` 當我們運行程序時,輸出將是: ```cs Enter integer value: 101 You entered 101 Enter double value: 59.412 You entered 59.412 ``` `Convert`類的`ToInt32()`和`ToDouble()`方法分別將輸入的字符串轉換為整數和雙精度類型。 同樣,我們可以將輸入轉換為其他類型。 這是`Convert`類的可用方法的[完整列表](https://msdn.microsoft.com/en-us/library/system.convert(v=vs.110).aspx "Convert methods")。 還有其他方法可以從用戶那里獲取數字輸入。 要了解更多信息,請訪問[從用戶輸入](https://stackoverflow.com/questions/24443827/reading-an-integer-from-user-input "Reading integer from user")讀取整數。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看