c# – 如何排除数组的值?

c# – 如何排除数组的值?,第1张

概述如何创建排除最低温度并计算平均温度的方法.我只想要一个提示,而不是完整的解决方案,因为我想自己解决我的编程问题.我只有大约10个班级…评论人们的评论我的教授没有讲课,我读过我的书多次回顾过它. 我让这个程序从用户那里拿一个号码.该数字将添加到数组中.该数组用于创建类Temp的实例以打印最低和最高临时值. class Program{ static void Main(string[] a 如何创建排除最低温度并计算平均温度的方法.我只想要一个提示,而不是完整的解决方案,因为我想自己解决我的编程问题.我只有大约10个班级…评论人们的评论我的教授没有讲课,我读过我的书多次回顾过它.

我让这个程序从用户那里拿一个号码.该数字将添加到数组中.该数组用于创建类Temp的实例以打印最低和最高临时值.

class Program{    static voID Main(string[] args)    {        Console.Write("Enter a Temperature in degrees:");        string n = Console.Readline();        int number = Convert.ToInt32( n);         Temp t = new Temp(100,52,98,30,11,54,number);        Console.Writeline("Lowest Temperature:{0}",t.lowest());        Console.Writeline("Highest Temperature: {0}",t.highest());        Console.Writeline("Average Temperature: {0}",t.Average());    }    public class Temp    {        private int[] temp = new int[7]; // array         public Temp(int d1,int d2,int d3,int d4,int d5,int d6,int d7) // constructor with 7 parameters        {            temp[0] = d1; // assigning constructor parameters to array            temp[1] = d2;            temp[2] = d3;            temp[3] = d4;            temp[4] = d5;            temp[5] = d6;            temp[6] = d7;        }        public int lowest() // returning the lowest value of the set of numbers        {            int smallest = 150;            for (int c = 0; c < 7; c++)            {                if (temp[c] < smallest)                {                    smallest = temp[c];                }            }            return smallest;        }        public int highest()        {            int highest = -1;            for (int c = 0; c < 7; c++)            {                if (temp[c] > highest)                {                    highest = temp[c];                }            }            return highest;        }        public double Average()        {            double average = 0;            for (int c = 0; c < 7; c++)            {            }            return average;        }    }}
解决方法 使用单个循环非常容易:
public double Average(){    // Initialize smallest with the first value.    // The loop will find the *real* smallest value.    int smallest = temp[0];    // To calculate the average,we need to find the sum of all our temperatures,// except the smallest.    int sum = temp[0];    // The loop does two things:    // 1. Adds all of the values.    // 2. Determines the smallest value.    for (int c = 1; c < temp.Length; ++c)    {        if (temp[c] < smallest)        {            smallest = temp[c];            }        sum += temp[c];    }    // The computed sum includes all of the values.    // Subtract the smallest.    sum -= smallest;    double avg = 0;    // and divIDe by (Length - 1)    // The check here makes sure that we don't divIDe by 0!    if (temp.Length > 1)    {        avg = (double)sum/(temp.Length-1);    }   return avg;}
总结

以上是内存溢出为你收集整理的c# – 如何排除数组的值?全部内容,希望文章能够帮你解决c# – 如何排除数组的值?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1262288.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存