static double ReadDouble()
{
char ch = '\0';
do
{
ch = (char)Console.Read();
} while (!(ch != '\t' && ch != '\n' && ch != ' '));
string str = "";
str += ch;
while (true)
{
ch = (char)Console.Read();
if (ch != '\t' && ch != '\n' && ch != ' ')
str += ch;
else
break;
}
return double.Parse(str);
}

解决方案 »

  1.   

    因为我之前是用C++的,习惯了一次性将多个数据在一行写完,但C#不行,所以自己写了个ReadDouble函数,但是因为最近才刚开始学,可能有很多函数不知道,所以用的也是最笨的方法,不知道大家能帮我改进一下吗?
      

  2.   

    既然是读取到换行符,那么你完全可以直接使用Console.ReadLine()读取一行字符串来实现。
      

  3.   

    不是,比如我连续读取三个数据,则Console.ReadLine();Console.ReadLine();Console.ReadLine();分别将三个字符串转换成对应的数值,但我输入的时候必须输入一个数值就按下Enter键,然后再输入,而不能一次性将三个输入完毕再按下Enter键(中间用空格或tab键隔开),写这个函数目的就是为了解决这个问题,但是我用的是最笨的方法,求更好的方法。
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                double d = ReadDouble();
                Console.WriteLine(d);
            }        private static double ReadDouble()
            {
                char ch = '\0';
                string s = "";
                while (true)
                {
                    ch = (char)Console.ReadKey().KeyChar;
                    if (new char[] { '\r', '\t', ' ' }.Contains(ch))
                    {
                        Console.WriteLine();
                        break;
                    }
                    s += ch.ToString();
                };
                return double.Parse(s);
            }
        }
    }
      

  5.   

    一样,只要对读取到的一行字符串做下处理即可,用string.Splite("",' ');分割读取到的字符串里面的所有数字。
      

  6.   

    一样,只要对读取到的一行字符串做下处理即可,用string.Splite("",' ');分割读取到的字符串里面的所有数字。噢,我试试。