日期:2014-05-17  浏览次数:20922 次

用C#写了个ReadDouble的函数,请大家给我改进一下
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);
}

------解决方案--------------------
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))
                {