-11

如何在循环中正确使用数组

static void Main(string[] args)
    {
        Temp t = new Temp( 100,52,98,30,11,54,87);
        Console.WriteLine(t.lowest());
    }
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 parametors
        {

            temp[0] = d1; // assigning constuctor parametors 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;

现在我的问题是不要做作业。但是要找到最高温度和平均值的问题。我会做另一个带有 intiatiilzing 的 for 循环,int highest = -1;然后做一些接近我为最小做的事情吗?

4

1 回答 1

1

find the highest temp?
只需将if ( temp[c] < smallest)您的方法的此条件更改public int lowest()if ( temp[c] > smallest),您将获得最高的。 您需要编写一个循环,将所有索引的值相加,然后将总和除以或者 我想为您提供示例代码,但我想这很容易,您应该自己做。我给出的想法就足够了

For Average?
7temp.Length;

于 2013-07-12T06:53:51.003 回答