using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using System.IO;namespace NunitTest
{
    public class MathCompute
    {
        public int Largest(int[] array)
        {
            if (null == array || 0 == array.Length)
            {
                throw new Exception("参数传递错误@!!!!!");
            }
            int largest = Int32.MinValue;
                        foreach (int element in array)
            {
                if (element > largest)
                {
                    largest = element;
                }
                
            }
            return largest;        }
    }
    [TestFixture]
    public class TestMathCompute
    {
        private MathCompute mc;
        [TestFixtureSetUp]
        public void init()
        {
            mc = new MathCompute();
        }
        [Test]
        public void TestLargest()
        {
            StreamReader sr = new StreamReader("../../DataSet.txt");            string line = null;            while (null != (line = sr.ReadLine()))
            {
                if (line.StartsWith("#"))
                {
                    continue;
                }
                string[] tokens = line.Split(null);                int expected = Int32.Parse(tokens[0]);                List<int> list = new List<int>();                for (int i = 1; i < tokens.Length; ++i)
                {
                    list.Add(Int32.Parse(tokens[i]));//运行时这里提示输入字符串的格式不正确!!请问问题在哪儿?
                }                int[] input = list.ToArray();                Assert.AreEqual(expected, mc.Largest(input));
            }
        }    }
}

解决方案 »

  1.   


    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using NUnit.Framework; 
    using System.IO; namespace NunitTest 

        public class MathCompute 
        { 
            public int Largest(int[] array) 
            { 
                if (null == array || 0 == array.Length) 
                { 
                    throw new Exception("参数传递错误@!!!!!"); 
                } 
                int largest = Int32.MinValue; 
                            foreach (int element in array) 
                { 
                    if (element > largest) 
                    { 
                        largest = element; 
                    } 
                    
                } 
                return largest;         } 
        } 
        [TestFixture] 
        public class TestMathCompute 
        { 
            private MathCompute mc; 
            [TestFixtureSetUp] 
            public void init() 
            { 
                mc = new MathCompute(); 
            } 
            [Test] 
            public void TestLargest() 
            { 
                StreamReader sr = new StreamReader("../../DataSet.txt");             string line = null;             while (null != (line = sr.ReadLine())) 
                { 
                    if (line.StartsWith("#")) 
                    { 
                        continue; 
                    } 
                    string[] tokens = line.Split(null);                 int expected = Int32.Parse(tokens[0]);                 List <int> list = new List <int>();                 for (int i = 1; i < tokens.Length; ++i) 
                    { 
                        list.Add(Int32.Parse(tokens[i]));//运行时这里提示输入字符串的格式不正确!!请问问题在哪儿? 
                    }                 int[] input = list.ToArray();                 Assert.AreEqual(expected, mc.Largest(input)); 
                } 
            }     } 
    }
      

  2.   

    调试一下,看看tokens[i]到底是什么东西?
      

  3.   

    tokens[i]是个空字符串吧,所以类型转换出错。。判断一下就行了。
      

  4.   

    楼主在string[] tokens = line.Split(null); 行设定一个断点,调试一把,应该是tokens数组有点问题,可能只有一个字符串或是没有字符串。
      

  5.   

    对于这个题,我认为出错的地方可能是tokens[i],他要么不是字符串,要么就为空!
      

  6.   

    哦,DataSet.txt文件也有可能出错!再看一下用指定方式分割后是否是整形的!