C# 中的控制檯輸入讀取方法
使用 ReadLine() 方法從 C# 中的控制檯讀取輸入。此方法接收輸入作為字串,因此您需要將其轉換。
例如 -
讓我們看看如何從使用者獲取使用者輸入並將其轉換為整數。
首先,讀取使用者輸入 -
string val;
Console.Write("Enter integer: ");
val = Console.ReadLine();現在將其轉換為整數 -
int a = Convert.ToInt32(val);
Console.WriteLine("Your input: {0}",a);讓我們在示例中瞭解這一點。使用命令列引數新增輸入 -
示例
using System;
using System.Collections.Generic;
class Demo {
static void Main() {
string val;
Console.Write("Enter Integer: ");
val = Console.ReadLine();
int a = Convert.ToInt32(val);
Console.WriteLine("Your input: {0}",a);
}
}上述程式碼將產生以下結果 -
Enter Integer: 5 Your input: 5
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP