微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#中的Console.Read()方法详解

Console.Read()方法用于从标准输入流中读取下一个字符。当用户键入一些输入字符时,此方法基本上阻止其返回。一旦用户按ENTER键,它就会终止。

语法:

public static int Read();

返回值:返回输入流中的下一个字符,如果当前没有要读取的字符,则返回负数(-1)。

例外:如果发生I / O错误,此方法将给出IOException。

以下程序说明了上述方法的使用:

示例1:

// C# program to illustrate the use 
// of Console.Read Method 
using System; 
  
namespace GFG { 
  
class Program { 
  
    static void Main(string[] args) 
    { 
          
        int x; 
        Console.WriteLine(Enter your Character to get Decimal number); 
  
        // using the method 
        x = Console.Read(); 
        Console.WriteLine(x); 
    } 
} 
}

输出

05bacc44ffc34e6a77f34a3a02e7bf7.png

示例2:

// C# program to illustrate the use 
// of Console.Read Method 
using System; 
  
namespace GFG { 
  
class Program { 
  
    static void Main(string[] args) 
    { 
        // Write to console window. 
  
        int x; 
        Console.WriteLine(Enter your Character to get Decimal number); 
        x = Console.Read(); 
        Console.WriteLine(x); 
  
        // Converting the decimal into character. 
        Console.WriteLine(Convert.tochar(x)); 
    } 
} 
}

输出

2cb0d4403b3b17b2c23996822faea0b.png

相关推荐:《C教程

本篇文章就是关于C#中的Console.Read()方法详解,希望对需要的朋友有所帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐