如何創建排除最低溫度並計算平均溫度的方法。我只想提示而不是完整的解決方案,因爲我想自己解決我的編程問題。我只有大約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;
}
}
}
+1:我比我所建議的更喜歡這個答案。 – Douglas