无涯教程-C# - ReadLine()方法

81 阅读2分钟

Console.WriteLine()是C#中使用的一种方法,用于打印单行的整个语句并将控制转移到控制台的下一行。与Console.WriteLine()类似,ReadLine()方法用于从用户读取整行字符串或语句值,直到按Enter键将控制转移到下一行。在本节中,无涯教程将详细了解ReadLine()、Read()Readkey()方法。

C# ReadLine() Method

Console.ReadLine()方法

它是Console类(System Namespace)的预定义方法。Console.ReadLine()方法从流输出设备(控制台)读取并仅返回字符串,直到找到换行符。如果希望从用户读取字符或数字值,则需要将字符串转换为适当的数据集。

语法

public static string ReadLine ();

该方法引发以下异常:

  1. IOException: 如果出现 I/O 错误,就会发生这种情况。
  2. OutOfMemoryException: 如果没有足够的内存来分配缓冲区以返回字符串,则会发生这种情况。
  3. ArgumentOutOfRangeException: 如果下一行中的字符数大于 MaxValue,则会发生这种情况。

示例1:让使用ReadLine()方法编写一个接受用户输入的程序。

Program.cs

using System; // 定义系统包
namespace ConsoleApp3 // 项目名称或文件夹
{
    class Program 
    {
        static void Main(string[] args) // 定义主函数
        {
            string name; // 字符串变量名
            Console.WriteLine("Hello, what is your name?"); 
            name = Console.ReadLine(); // 接受用户的输入
            Console.WriteLine("Hi! "+ name + " Welcome to the Learnfk"); // 打印输出
        }
    }
}

输出

C# ReadLine() Method

示例2:使用C#中的ReadLine()函数编写程序打印用户的名字和姓氏。

Program2.cs

using System;

namespace ConsoleApp3 { class Program2 { static void Main(string[] args) { string fname, lname; // 字符串变量 Console.Write("Please, Enter your first Name : "); fname = Console.ReadLine(); // 取用户的名字

        </span><span class="com">// ReadLine() 是 Console 类从标准输入流中读取一行的方法</span><span class="pln">
        </span><span class="typ">Console</span><span class="pun">.</span><span class="typ">Write</span><span class="pun">(</span><span class="str">"Please, Enter Your Last Name : "</span><span class="pun">);</span><span class="pln">
        lname </span><span class="pun">=</span><span class="pln"> </span><span class="typ">Console</span><span class="pun">.</span><span class="typ">ReadLine</span><span class="pun">();</span><span class="pln"> </span><span class="com">// 从用户那里取第二个名字</span><span class="pln">

        </span><span class="typ">Console</span><span class="pun">.</span><span class="typ">WriteLine</span><span class="pun">(</span><span class="str">"Your Full Name is : "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> fname </span><span class="pun">+</span><span class="pln"> </span><span class="str">" "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> lname</span><span class="pun">);</span><span class="pln">
    </span><span class="pun">}</span><span class="pln">
</span><span class="pun">}</span><span class="pln">

}

输出

C# ReadLine() Method

read()方法

C#中的read()方法用于从用户读取单个字符。它与readLine()方法不同,因为readLine()方法接收用户输入的每一行,直到该行结束,控制转移到下一条语句读取字符串。

Program4.cs

using System; // 定义系统包
namespace ConsoleApp3
{
    class Program4 
    {
        static void Main(string[] args) 
        {
            char ch;
            Console.Write("Enter the characters "); // Cosole.Write() 打印同一行语句。
            ch = Convert.ToChar(Console.Read()); //从用户那里读取一个字符。
            Console.WriteLine("You have entered the character " + ch); //打印整行
        }
    }
}

输出

C# ReadLine() Method

ReadKey()

ReadKey()方法用于获取下一个字符,或者用户按任意键退出程序。它会一直按住屏幕,直到用户按下键盘上的任意键。按下的键将显示在控制台上。

Program5.cs

using System; // 定义系统包
namespace ConsoleApp3
{
    class Program5 
    {
        static void Main(string[] args) 
        {     
             DateTime dt = DateTime.Now; // DateTime.Now() 打印当前时间
             Console.WriteLine(" The Current Date and Time is : " + dt);
             Console.Write("Press any key or Enter to exit from the Console Screen");
             Console.ReadKey(); // 输入任意键退出控制台屏幕。
            }
    }
}

输出

C# ReadLine() Method

参考链接

www.learnfk.com/csharp/c-sh…