console.read()读入的内容

  今天写的特别简单的代码,大体是一个模式选择,从控制台读入一个数,然后做出相应的选择.

  代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 贴吧发帖统计
{
    class Program
    {
        static void Main(string[] args)
        {
            
            while (true)
            {
                int model = 0;
                _put("model:");
                model = Console.Read();
                //_put(model.ToString());
                if (model < 48 || model > 57) 
                    continue;

                switch (model)
                {
                    case 1: _put(model.ToString()); break;
                    default: _put(model.ToString()); break;
                }
            }           
        }

        static void _put(string s)
        {
            Console.WriteLine(s);
        }


    }
}

  然后每次读入一个model,都会输出一个数和另外两个"model:",百思不得其解,如下图:

  

  逐行调试,发现model每次都会多两个值"0x0000000d"和"0x0000000a",16进制的13跟10,一个是回车符,一个是换行符.

  

  

 

  从网上看了一些关于换行符("\n",值为10)跟回车符("\r",值为13)的概念.

  Unix 系统里,每行结尾只有“<换行>”,即“\n”;Windows系统里面,每行结尾是“ <回车><换 行>”,即“\r\n”;Mac系统里,每行结尾是“<回车>”。一个直接后果是,Unix/Mac系统下的文件在Windows里打 开的话,所有文字会变成一行;而Windows里的文件在Unix/Mac下打开的话,在每行的结尾可能会多出一个^M符号.

 

 

  但是,使用console.readLine()函数,就可以避免读入"回车符"跟"换行符".

posted on 2014-05-01 03:19  13m0n  阅读(850)  评论(0编辑  收藏  举报