c#控制台应用程序如何输入多行输入而不只是读取ReadLine中的第一行?

时间:2014-12-14 14:56:35

标签: c# console-application

我在c#中创建一个程序,它将读取多行并一次读取一行并输出一些信息。代码如下:

static void Main(string[] args)
        {            
            string info1Stuff = Console.ReadLine();
            string[] theInfo1 = info1Stuff.Split(',');            

            string info2Stuff = Console.ReadLine();
            string[] theInfo2 = info1Stuff.Split(',');
            string info3Stuff = Console.ReadLine();
            string[] theInfo3 = info1Stuff.Split(',');
            string info4Stuff = Console.ReadLine();
            string[] theInfo4 = info1Stuff.Split(',');
            string info5Stuff = Console.ReadLine();
            string[] theInfo5 = info1Stuff.Split(',');

            NavigationInfo info1 = new NavigationInfo(theInfo1[0], theInfo1[1], theInfo1[2], theInfo1[3], Convert.ToDouble(theInfo1[4]));

            info1.calculateDistance();
            info1.calculateTime();
            info1.calculateCost();

            NavigationInfo info2 = new NavigationInfo(theInfo2[0], theInfo2[1], theInfo2[2], theInfo2[3], Convert.ToDouble(theInfo2[4]));

            info2.calculateDistance();
            info2.calculateTime();
            info2.calculateCost();

            NavigationInfo info3 = new NavigationInfo(theInfo3[0], theInfo3[1], theInfo3[2], theInfo3[3], Convert.ToDouble(theInfo3[4]));

            info3.calculateDistance();
            info3.calculateTime();
            info3.calculateCost();

            NavigationInfo info4 = new NavigationInfo(theInfo4[0], theInfo4[1], theInfo4[2], theInfo4[3], Convert.ToDouble(theInfo4[4]));

            info4.calculateDistance();
            info4.calculateTime();
            info4.calculateCost();

            NavigationInfo info5 = new NavigationInfo(theInfo5[0], theInfo5[1], theInfo5[2], theInfo5[3], Convert.ToDouble(theInfo5[4]));

            info5.calculateDistance();
            info5.calculateTime();
            info5.calculateCost();

            Console.WriteLine(info1.ToString());
            Console.WriteLine(info2.ToString());
            Console.WriteLine(info3.ToString());
            Console.WriteLine(info4.ToString());
            Console.WriteLine(info5.ToString());

但是,当我输入所有数据时,它会显示第一行输入的输出5次,而不是5种不同的输出行。所以它基本上忘记了第2-5行,只使用第一行输入并输出了5次

请告诉我

由于 罗汉

3 个答案:

答案 0 :(得分:1)

这应该做的工作:

string info1Stuff = Console.ReadLine();

string[] theInfo1 = info1Stuff.Split(',');            


string info2Stuff = Console.ReadLine();

string[] theInfo2 = info2Stuff.Split(',');

string info3Stuff = Console.ReadLine();

string[] theInfo3 = info3Stuff.Split(',');

string info4Stuff = Console.ReadLine();

string[] theInfo4 = info4Stuff.Split(',');

string info5Stuff = Console.ReadLine();

string[] theInfo5 = info5Stuff.Split(',');

答案 1 :(得分:1)

单独处理五个不同的信息位似乎使事情变得很麻烦。使用info数组。这使您可以使用循环。

const N = 5;  // Number of input lines.

var info = new NavigationInfo[N];  // Here an array is handier as individual variables.

for (int i = 0; i < N; i++) {
     // You don't need any index here; you can reuse the same variable.
    string input = Console.ReadLine();

    // Same here. You can reuse this array.
    string[] parts = input.Split(',');

    var navInfo = new NavigationInfo(parts[0], parts[1], parts[2], parts[3],
                                     Convert.ToDouble(parts[4]));
    navInfo.calculateDistance();
    navInfo.calculateTime();
    navInfo.calculateCost();

    info[i] = navInfo;
}
for (int i = 0; i < N; i++) {
    Console.WriteLine(info[i]);  // Console.WriteLine calls ToString automatically.
}

许多软件开发原则之一被命名为 DRY D R epeat Y 我们自己。处理五条输入线分别重复几乎相同的代码5次。这是一个很多打字,给你5次错误的机会。此外,如果您需要稍后调整代码,则需要执行5次。

答案 2 :(得分:0)

改变这个:

        string[] theInfo1 = info1Stuff.Split(',');            

        string info2Stuff = Console.ReadLine();
        string[] theInfo2 = info1Stuff.Split(',');
        string info3Stuff = Console.ReadLine();
        string[] theInfo3 = info1Stuff.Split(',');
        string info4Stuff = Console.ReadLine();
        string[] theInfo4 = info1Stuff.Split(',');
        string info5Stuff = Console.ReadLine();
        string[] theInfo5 = info1Stuff.Split(',');

到此:

        string[] theInfo1 = info1Stuff.Split(',');            

        string info2Stuff = Console.ReadLine();
        string[] theInfo2 = info2Stuff.Split(',');
        string info3Stuff = Console.ReadLine();
        string[] theInfo3 = info3Stuff.Split(',');
        string info4Stuff = Console.ReadLine();
        string[] theInfo4 = info4Stuff.Split(',');
        string info5Stuff = Console.ReadLine();
        string[] theInfo5 = info5Stuff.Split(',');

这是一个简单的复制/粘贴错误。