Console.Read()和Console.ReadLine()问题

我一直在尝试在C#中使用Console.Read()和Console.ReadLine(),但结果却变得奇怪。 例如这段代码

Console.WriteLine("How many students would you like to enter?"); int amount = Console.Read(); Console.WriteLine("{0} {1}", "amount equals", amount); for (int i=0; i < amount; i++) { Console.WriteLine("Input the name of a student"); String StudentName = Console.ReadLine(); Console.WriteLine("the Students name is " + StudentName); } 

当我输入1学生人数时,我已经给了我这个金额= 49,我甚至没有机会输入学生姓名。

这是因为你读了一个char。 使用适当的方法,如ReadInt32() ,负责从读取符号到您希望的类型的正确转换。

你得到49的原因是因为它是’1’符号的字符代码,而不是它的整数表示。

 char code 0 : 48 1 : 49 2: 50 ... 9: 57 

例如: ReadInt32()可能如下所示:

 public static int ReadInt32(string value){ int val = -1; if(!int.TryParse(value, out val)) return -1; return val; } 

并使用这个像:

 int val = ReadInt32(Console.ReadLine()); 

有可能创建extension method真的很好,但不幸的是,无法在静态类型上创建扩展方法,而Console是static类型。

你从read而不是int得到一个字符char 。 您需要先将其作为字符串并将其解析为字符串。 实现可能如下所示

  Console.WriteLine("How many students would you like to enter?"); var read = Console.ReadLine(); int amount; if(int.TryParse(read,out amount)) { Console.WriteLine("{0} {1}", "amount equals", amount); for (int i=0; i < amount; i++) { Console.WriteLine("Input the name of a student"); String StudentName = Console.ReadLine(); Console.WriteLine("the Students name is " + StudentName); } } 

我已将其更改为使用readline,因为readline返回一个字符串并不会随意将学生数量限制为9(一位数的最大数字)

尝试以这种方式更改您的代码

 int amount; while(true) { Console.WriteLine("How many students would you like to enter?"); string number = Console.ReadLine(); if(Int32.TryParse(number, out amount)) break; } Console.WriteLine("{0} {1}", "amount equals", amount); for (int i=0; i < amount; i++) { Console.WriteLine("Input the name of a student"); String StudentName = Console.ReadLine(); Console.WriteLine("the Students name is " + StudentName); } 

而是使用Read使用ReadLine ,然后使用Int32.TryParse检查用户输入是否真的是整数。 如果用户未输入有效数字,请重复该问题。
使用Console.Read会将输入限制为需要转换并检查为有效数字的单个char。

当然,这是一个残酷的例子,没有任何错误检查或任何类型的安全中止循环。

对于可能仍需要此人的人:

 static void Main(string[] args) { Console.WriteLine("How many students would you like to enter?"); var amount = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("{0} {1}", "amount equals", amount); for (int i = 0; i < amt; i++) { Console.WriteLine("Input the name of a student"); String StudentName = Console.ReadLine(); Console.WriteLine("the Students name is " + StudentName); } } 

Console.Read()返回您输入的字符的char代码。 你需要使用Convert.ToChar(amount); 将字符作为字符串,然后你需要执行int.Parse()来获取你正在寻找的值。

Console.Read返回按下的关键字符的asci值。

如果你使用Console.ReadKey().KeyChar你会得到一个代表按下的实际角色的字符。

然后,您可以使用.ToString()将该字符转换为一个字符串。

现在您有了一个字符串,您可以使用int.Parseint.TryParse将包含完全数字字符的字符串转换为整数。

所以把它们放在一起:

 int value; if (int.TryParse(Console.ReadKey().KeyChar.ToString(), out value)) { //use `value` here } else { //they entered a non-numeric key } 

代替:

 int amount = Console.Read(); 

尝试:

 int amount = 0; int.TryParse(Console.ReadLine(), out amount); 

因为你只读了字符代码,所以例如输入11你仍然会得到49.你需要读取字符串值并将其解析为int值。 如果输入错误,上面的代码你会得到0。

试试这个:

 int amount = ReadInt32(); 

或者如果它不起作用试试这个:

 int amount = Console.ReadInt32(); 

或这个:

 int amount = Convert.ToInt32(Console.Readline()); 

在这里,它将读取字符串然后它将它转换为Int32值。

您也可以访问: http : //www.java2s.com/Tutorials/CSharp/System.IO/BinaryReader/C_BinaryReader_ReadInt32.htm

如果没有任何作用,请告诉我。

 Console.WriteLine("How many students would you like to enter?"); string amount = Console.ReadLine(); int amt = Convert.ToInt32(amount); Console.WriteLine("{0} {1}", "amount equals", amount); for (int i = 0; i < amt; i++) { Console.WriteLine("Input the name of a student"); String StudentName = Console.ReadLine(); Console.WriteLine("the Students name is " + StudentName); } //thats it 

TL; DR; Windows中的Enter键不是单个字符。 这个事实是与Console.Read()方法相关的许多问题的根源。

完整的细节 :如果您在计算机上运行下面的代码,那么您可以解决Console.Read()背后的许多谜团:

 static void Main(string[] args) { var c = Console.Read(); Console.WriteLine(c); c = Console.Read(); Console.WriteLine(c); } 

在运行程序时,只需在键盘上按Enter键一次,然后在控制台上检查输出。 以下是它的外观:

在此处输入图像描述

有趣的是,你只按了一次Enter键,但它能够满足我的代码片段中的两个Read()调用。 在Windows中输入密钥会为新行字符发出两个字符,即回车符( \r \n ASCII代码13)和换行符( \n – ASCII代码10)。 你可以在这篇文章中阅读更多内容 – Windows回车\ r \ n是由两个字符还是一个字符组成?