Console.ReadLine add 48 to int

家住魔仙堡 提交于 2019-12-29 09:08:13

问题


I get 48 when I input 0 to a ReadLine().

Is this a bug?

class Program
{
    static void Main(string[] args)
    {
        string name;
        int age;

        readPerson(out name, out age);
    }
    static void readPerson(out string name, out int age)
    {
        Console.Write("Enter name: ");
        name = Console.ReadLine();
        Console.Write("Enter age: ");
        age = Console.Read();
        Console.WriteLine("Name: {0}; Age: {1}", name, age.ToString());
    }
}

回答1:


According to the MSDN documentation, the Console.Read method returns:

The next character from the input stream, or negative one (-1) if there are currently no more characters to be read.

So, really what you're seeing is only the first character currently on the stream (i.e., characters received between the last two Enter pushes).

During your unit testing, it appeared as if the values were shifted by 48, because it so happens that the ASCII values for the characters from '0' to '9' are, you guessed it, 48 for '0', 49 for '1', etc:

Since you didn't specify a conversion, stream contents were "automagically" read as char values, and your call to Read() displayed their ASCII decimal equivalents.

You can verify this using this simple test:

static void TestRead()
{
    int current = 0;

    Console.Write("Enter 1: ");
    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    Console.Write("Enter 22: ");
    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);
}

Which will result in:

You will notice that back-to-back calls to Read() grab a single character from the stream, and give you its ASCII decimal equivalent. Also, note how Windows appends a carriage return (ASCII 13) and linefeed (ASCII 10) sequence for every stroke of the Enter key, which your program faithfully echoes back to you.

A slight modification of this test method would help drive the point that lacking specific directions, the runtime will interpret the contents of your input stream as characters:

static void TestReadModified()
{
    int current = 0;

    Console.Write("Enter a: ");
    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);

    current = Console.Read();
    Console.WriteLine("Next char: {0}", current);
}

As expected, the method above will return the ASCII value for character 'a':

As others have already mentioned, this is easy to fix. Just inform the runtime that you want the value to be interpreted as an int. It's probably also a good idea to, at least, check that the input received is a number:

static void readPerson(out string name, out int age)
{
    Console.Write("Enter name: ");
    name = Console.ReadLine();

    Console.Write("Enter age: ");

    // in this case, we could simply use tempAge (defaults to 0)
    // but it's just practice to check TryParse's success flag
    int tempAge;
    var success = Int32.TryParse(Console.ReadLine(), out tempAge);

    age = success ? tempAge : 0;

    Console.WriteLine("Name: {0}; Age: {1}", name, age);
    Console.ReadLine();
}



回答2:


No, it's not a bug at all.

Console.Read() returns the ASCII character code for whatever character is entered. The ASCII code for the digit 0 is 48, and for the digit 1 is 49, and so on. It's not adding the number 48 arbitrarily, nor does it have anything to do with out parameters.

You need to read in a line and cast the input to an integer accordingly:

Console.Write("Enter age: ");
age = Convert.ToInt32(Console.ReadLine());

If you need to use Read() for whatever reason, then like I said in my comment you need to cast the result to a char. You'll also need to change your variable from int age to char age:

class Program
{
    static void Main(string[] args)
    {
        string name;
        char age;

        readPerson(out name, out age);
    }
    static void readPerson(out string name, out char age)
    {
        Console.Write("Enter name: ");
        name = Console.ReadLine();
        Console.Write("Enter age: ");
        age = (char) Console.Read();
        Console.WriteLine("Name: {0}; Age: {1}", name, age.ToString());
    }
}

Bear in mind that Read() can only read one character at a time, so if you need to parse an age with more than one digit, this won't work, and you're much better off just using ReadLine() instead.




回答3:


The ASCII codes for the numbers are being printed. Note that Console.Read() reads the next character from the standard input and returns an int. So, when you input character 0 ( not integer 0), it returns the ASCII code 48 and so on. Also, this ASCII code is the integer code for the character ( say 0) so converting to int of this code is redundant. You read in the age as string and convert it to integer, which can be done with Console.ReadLine and then using int.TryParse etc.



来源:https://stackoverflow.com/questions/9156363/console-readline-add-48-to-int

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!