到Console.ReadLine添加48到int [英] Console.ReadLine add 48 to int

查看:225
本文介绍了到Console.ReadLine添加48到int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到48,当我输入0到的ReadLine()。



这是一个错误吗?



 类节目
{
静态无效的主要(字串[] args)
{
字符串名称;
INT年龄;

readPerson(出姓名,年龄出来);
}
静态无效readPerson(出字符串名称,OUT INT岁)
{
Console.Write(输入名称:);
NAME =到Console.ReadLine();
Console.Write(请输入年龄:);
年龄= Console.Read();
Console.WriteLine(名称:{0};年龄:{1},名称,age.ToString());
}
}


解决方案

据MSDN文档时,的 Console.Read 方法的返回值:




从输入流,或负一(-1)的下一个字符,如果目前还没有更多的字符是阅读。




所以,真的是你看到的仅仅是第一个字符当前在流(即最后的接收字符两个<大骨节病>输入推)。



在你的单元测试,其表现为,如果值由48转移,因为它恰巧的ASCII从0到9是字符的值,你猜对了,48'0',49'1',等等:





既然你没有指定一个转换,流内容是自动地读成字符值,您的来电阅读()显示其ASCII十进制等效。



您可以在此使用这个简单的测试验证:

 静态无效TestRead()
{
INT电流= 0;

Console.Write(输入1:);
电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

Console.Write(请输入22:);
电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);
}



这将导致:





您会发现,背到后端调用阅读()抓住从流中单个字符,给你的ASCII十进制等效值。另外,还要注意如何Windows附加的<大骨节病>输入键的每一个行程,你的程序如实地回送你一个回车(ASCII 13)和换行(ASCII 10)序列。



这种测试方法的一个稍微的改变,将有助于推动与缺少具体的方向,运行时将解释你的输入流的字符的内容点:

 静态无效TestReadModified()
{
INT电流= 0;

Console.Write(请输入:);
电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);

电流= Console.Read();
Console.WriteLine(下一个字符:{0},电流);
}



正如预期的那样,上面的方法将返回字符的ASCII值'a'





正如其他人已经提到,这是很容易解决。只是通知你想要的值被解释为 INT 运行。这可能也是一个好主意,至少,检查接收到的输入是一个数字:

 静态无效readPerson(出字符串姓名,出INT岁)
{
Console.Write(输入名称:);
NAME =到Console.ReadLine();

Console.Write(请输入年龄:);

//在这种情况下,我们可以简单地使用tempAge(默认为0)
//但它只是练习来检查的TryParse的成功标志
INT tempAge;
VAR成功= Int32.TryParse(到Console.ReadLine(),出tempAge);

年龄=成功? tempAge:0;

Console.WriteLine(名称:{0};年龄:{1},姓名,年龄);
到Console.ReadLine();
}


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

解决方案

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

这篇关于到Console.ReadLine添加48到int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆