Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Max打印整个数组,而不是最大值(C#)_Arrays_Max_System.io.file_Converters - Fatal编程技术网

Arrays Max打印整个数组,而不是最大值(C#)

Arrays Max打印整个数组,而不是最大值(C#),arrays,max,system.io.file,converters,Arrays,Max,System.io.file,Converters,我的作业是写两个主要部分的代码:一个是创建一个包含名称和数字的文本文件,另一个是读取相同的文本文件,并打印出文件中最大的数字。 这是创建文本文件的代码部分(我看不出有任何问题,它工作得很好), 课程为: class Person { public string Name; public string Age; } 主要问题是: Person a = new Person(); Person b = new Person(); Pe

我的作业是写两个主要部分的代码:一个是创建一个包含名称和数字的文本文件,另一个是读取相同的文本文件,并打印出文件中最大的数字。 这是创建文本文件的代码部分(我看不出有任何问题,它工作得很好), 课程为:

class Person
{
    public string Name;
    public string Age;
}
主要问题是:

        Person a = new Person();
        Person b = new Person();
        Person c = new Person();
        a.Name = "Abel";
        a.Age = "20";
        b.Name = "Bob";
        b.Age = "22";
        c.Name = "Cain";
        c.Age = "25";
        string[] People = { a.Name, a.Age, b.Name, b.Age, c.Name, c.Age };

        using (StreamWriter write = new StreamWriter(@"C:\Users\A\Desktop\file check\test.txt"))
        {
            for (int i = 0; i < People.Length; i++)
            {
                write.WriteLine(People[i]);
            }
        }
这部分工作正常。 我遇到问题的部分是我应该读取文件并打印最大数字的部分,如下所示:

Abel
20
Bob
22
Cain
25
        string PeopleCheck = @"C:\Users\A\Desktop\file check\test.txt";
        using (StreamReader read = new StreamReader(PeopleCheck))
        {
            while (true)
            {
                string FindMax = read.ReadLine();
                if (FindMax == null)
                {
                    break;
                }   
                int test;
                if(Int32.TryParse(FindMax, out test))
                {
                   // Console.WriteLine(FindMax); --> this will print all numbers, one number in each line
                    int[] numbers = FindMax.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
                    Console.WriteLine("the highest number is {0}", numbers.Max());
                }
            }
        }
    }
the highest number is 20
the highest number is 22
the highest number is 25
Press any key to continue . . .
我用了这个帖子: ,将字符串转换为数字数组。我以为numbers.Max()会打印出最大的数字,但输出结果如下所示:

Abel
20
Bob
22
Cain
25
        string PeopleCheck = @"C:\Users\A\Desktop\file check\test.txt";
        using (StreamReader read = new StreamReader(PeopleCheck))
        {
            while (true)
            {
                string FindMax = read.ReadLine();
                if (FindMax == null)
                {
                    break;
                }   
                int test;
                if(Int32.TryParse(FindMax, out test))
                {
                   // Console.WriteLine(FindMax); --> this will print all numbers, one number in each line
                    int[] numbers = FindMax.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
                    Console.WriteLine("the highest number is {0}", numbers.Max());
                }
            }
        }
    }
the highest number is 20
the highest number is 22
the highest number is 25
Press any key to continue . . .

如果有人知道如何解决这个问题,使输出为单个数字,我将非常感激,提前感谢任何试图帮助我的人。

您正在逐个读取整数,逐个解析它们,并为每个整数打印您已处理的整数的当前最大值

这是一个巧合,因为列表是按升序排列的,所以列表没有变化。如果您尝试
3,2,1
,它将打印
3,3,3

循环后打印一次


将来,您可以使用调试器解决类似这样的问题。通过单步执行代码很容易看到。

我不确定我是否理解您的意思。。。您说过要从循环中打印一次,但在循环之外不存在数组。我用调试器注意到了我的问题所在,但我真的不知道如何解决它…@TonySlepian你能发布一个示例文本文件吗。在那之后,这个问题对你仍然有意义吗?我现在才注意到两周过去了。它仍然是相关的,我将把文本文件添加到主要问题集合应该在循环之外(我没有注意到这个问题)。尝试一个
列表
,并用你解析的每一行来填充它。我不知道怎么做,但我会问我的老师如何编写你的方法,谢谢你的帮助^^