2013-07-09 24 views
-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; 

現在我的問題是不是做功課。但是要找到最高溫度和平均值的問題。 我會做一個循環與暗殺int highest = -1;然後做一些接近我做的最小?

+1

聽起來像是我的一個班級任務...你到目前爲止嘗試過什麼? – Kevin

+0

我想我會按照規範對它進行編碼。向我們展示一些代碼,我們可以幫助您解決它。 –

+1

@zach,在我看來,你已經在作業任務中添加了「你會怎樣做這個..」。這不是一個「爲我做作業」的網站。這是一個「我怎麼解決這個問題,我卡住了」網站。 –

回答

1

find the highest temp?
只要改變你的方法public int lowest()的這個條件if (temp[c] < smallest)if (temp[c] > smallest),你會得到最高的。

For Average?
你需要寫一個循環添加所有指標的值,然後通過7temp.Length;

我還以爲你提供的示例代碼劃分的總和,但我想它是那麼容易的,你應該自己動手。我給出的想法已經足夠了