繁体   English   中英

C#使用文本文件输出最大最小平均数组?

[英]C# Output max min average array using a text file?

我之前做过练习,因此我必须编写一个程序,用户将输入30个不同的数字(小时),并且该程序将吐出数组,平均值,最小值和最大值。 很好 但是现在,我的任务是从文本文件中读取数字(并输出与以前相同的值-数组,平均,最小,最大)。

我在基于原始练习的过程中遇到错误,并被告知该程序中不存在名称(我理解这是因为文本doc现在拥有该先前名称)。 我不知道该做些什么。

我看了一些视频,发现此站点上列出了类似的问题,但卡住了:(。有人可以帮忙吗?

程序代码为:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace IntsArray
{
    class Program
    {
        static void Main(string[] args)
        {

            StreamReader myReader = new StreamReader("Values.txt");
            string line = "";

            while (line != null)
            {
                line = myReader.ReadLine();
                if (line != null)
                    Console.WriteLine(line);
            }
            myReader.Close();
            Console.ReadLine();

            for (int index = 0; index < hoursArray.Length; index++)
            {
                Console.Write("Enter your hours: ");
                hoursArray[index] = int.Parse(Console.ReadLine());
            }
            Console.WriteLine("Numbers in the list: " + hoursArray.Length);
            for (int index = 0; index < hoursArray.Length; index++)
            {
                Console.WriteLine(hoursArray[index]);
            }
            Console.ReadKey();

            int total = 0;      
            double average = 0; 
            for (int index = 0; index < hoursArray.Length; index++)
            {
                total = total + hoursArray[index];
            }

            average = (double)total / hoursArray.Length;
            Console.WriteLine("Average = " + average.ToString("N2"));


            int high = hoursArray[0]; 

            for (int index = 1; index < hoursArray.Length; index++)
            {
                if (hoursArray[index] > high)
                {
                    high = hoursArray[index];
                }
            }

            Console.WriteLine("Highest number = " + high);


            int low = hoursArray[0];   

            for (int index = 0; index < hoursArray.Length; index++)
            {
                if (hoursArray[index] < low)
                {
                    low = hoursArray[index];
                }
            }

            Console.WriteLine("Lowest number = " + low);
            Console.ReadKey();

        }
    }
}

Values.txt中的数字如下所示:

8
24
9
7
6
12
10
11
23
1
2
9
8
8
9
7
9
15
6
1
7
6
12
10
11
23
1
2
9
8

我了解问题,只是不知道如何解决!

如果要从文件中读取数字,为什么要使用此代码?

for (int index = 0; index < hoursArray.Length; index++)
{
    Console.Write("Enter your hours: ");
    hoursArray[index] = int.Parse(Console.ReadLine());
}

您可以做的是从文件中读取数字,将其存储在ArrayList ,然后使用Linq获得MaxMinAverage

List<int> Numbers = System.IO.File.ReadAllLines("Your File Path")
                   .Select(N=> Convert.ToInt32(N)).ToList();

Console.WriteLine(Numbers.Average());
Console.WriteLine(Numbers.Max());
Console.WriteLine(Numbers.Min());

您需要定义数组

int[] hoursArray = new int[WHATEVER LENGTH IT SHOULD BE];

那么您的StreamReader读取行内容必须看起来像这样:

string line = "";
int position = 0; //needed to set the array position
while (line != null)
{
    line = myReader.ReadLine();
    if (line != null)
    {
        Console.WriteLine(line);
        hoursArray[position] = int.Parse(line); //convert line to int and sotres it in the array
        position++;
    }
}

如果您想使其更加灵活,我将使用列表而不是数组

编辑:最小最大平均计算:

int max = 0;
int min = 9999999;
double avg = 0;

for (int i = 0; i < hoursArray.Length; i++)
{
    if (hoursArray[i] > max) //if there is a higher value then the current max
        max = hoursArray[i];

    if (hoursArray[i] < min) //if there is a lower value then the current min
        min = hoursArray[i];

    avg = avg + hoursArray[i];
}
avg = avg / hoursArray.Length;

Console.WriteLine("Max: " + max);
Console.WriteLine("Min: " + min);
Console.WriteLine("Avg: " + avg);
Console.ReadLine();

您只需要将它们放在您的主体中就可以了 在此处输入图片说明

您要做的第一件事是识别文本文件中的行数。

var file = new StreamReader("file.txt").ReadToEnd(); 
var lines = file.Split(new char[] {'\n'});  
var count = lines.Count;

基于此计数初始化数组

int[] hoursArray = new int[count];

之后,填充您的数组。

int index = 0;
while ((line = sr.ReadLine()) != null) 
{
    hoursArray[index++] = Convert.ToInt32(line);
}

然后,在最后一步,您现在可以应用linq来获得与arvin所说的相同的max min和avg。

var max = hoursArray.Max();
var min = hoursArray.Min();
var avg = hoursArray.Avg();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM