6

I have been trying to use Console.Read() and Console.ReadLine() in C# but have been getting weird results. for example this code

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

has been giving me that amount = 49 when I input 1 for the number of students, and Im not even getting a chance to input a student name.

4

10 回答 10

11

这是因为您阅读了一个字符。使用类似的适当方法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类型。

于 2012-09-06T20:48:01.203 回答
3

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

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

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

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

于 2012-09-06T20:50:38.377 回答
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);
     }
}
于 2016-02-03T01:50:21.727 回答
2

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

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

于 2012-09-06T20:54:35.350 回答
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
于 2014-02-12T15:18:59.560 回答
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
}
于 2012-09-06T20:57:58.110 回答
0

代替:

int amount = Console.Read();

尝试:

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

这是因为您只读取了字符代码,因此例如键入 11 您仍然会得到 49。您需要读取字符串值并将其解析为 int 值。在输入错误的情况下使用上面的代码您将得到 0。

于 2012-09-06T20:56:03.427 回答
0

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

于 2012-09-06T20:49:25.150 回答
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

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

于 2015-07-06T10:57:07.213 回答
0

TL;博士; EnterWindows 中的键不是单个字符。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()我的代码片段中的两个调用。EnterWindows 中的 key 为换行符发出两个字符,即回车符(\r- ASCII 代码 13)和换行符(\n- ASCII 代码 10)。您可以在这篇文章中了解更多信息 - Windows 回车符 \r\n 是由两个字符还是一个字符组成?

于 2018-05-24T08:53:35.690 回答