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

时间:2012-09-06 20:45:56

标签: c# .net console-application

我一直在尝试在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,我甚至没有机会输入学生姓名。

10 个答案:

答案 0 :(得分:9)

这是因为你读了一个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非常好,但不幸的是,无法在静态类型上创建扩展方法,而Consolestatic类型。

答案 1 :(得分:3)

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

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);
     }
}

答案 2 :(得分:2)

尝试以这种方式更改代码

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会将您的输入限制为需要转换并检查为有效数字的单个字符。

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

答案 3 :(得分:2)

你从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(一位数的最大数字)

答案 4 :(得分:0)

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

答案 5 :(得分:0)

而不是:

int amount = Console.Read();

尝试:

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

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

答案 6 :(得分:0)

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

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

然后,您可以使用.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
}

答案 7 :(得分:0)

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

答案 8 :(得分: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

如果无效,请告诉我。

答案 9 :(得分:0)

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

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

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

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

enter image description here

有趣的是,您只按了一次 Enter 键,但它能够满足我的代码段中的两个Read()次调用。 在Windows中输入键会为新行字符发出两个字符,即回车符(\r - ASCII码13)和换行符(\n - ASCII码10)。您可以在此帖子中详细阅读相关内容 - Does Windows carriage return \r\n consist of two characters or one character?

相关问题