using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo1
{
class Program
{
/// <summary>
/// 求一個字符串數組中最長的元素
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string GetLongest(string[] s)
{
string max = s[0];
for (int i = 0; i < s.Length; i++)
{
if (s[i].Length > max.Length)
{
max = s[i];
}
}
return max;
}
static void Main(string[] args)
{
//用方法來實現:有一個字符串數組:
//{"馬龍","邁克爾喬丹","雷吉米勒","科比"},請輸出最長的那個
string[] names = { "馬龍", "邁克爾喬丹", "雷吉米勒", "科比" };
Console.WriteLine(GetLongest(names));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo1
{
class sumnumber
{
/// <summary>
/// 計算出整數數組的平均值並保留2位小數
/// </summary>
/// <param name="nums"></param>
/// <returns></returns>
public double GetAvg(int[] nums)
{
double sum = 0;
for (int i = 0; i < nums.Length;i++ )
{
sum += nums[i];
}
return sum / nums.Length;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Demo1
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 7 };
sumnumber sber = new sumnumber();
double avg = sber.GetAvg(numbers);
//保留2位小數 將avg轉換成string類型
//然後用Convet.ToDouble轉換
string str=avg.ToString("0.00");
avg = Convert.ToDouble(str);
Console.WriteLine(avg);
Console.ReadKey();
}
}
}