C# 數組寫成績的總分與平均分

定義一個數組,長度不限,要求用戶輸入成績並存儲
輸出總成績、平均成績
進行異常處理

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSbaseDemo2
{
    class Program
    {
        static void Main(string[] args)
        {
			try
			{
				int[] arr = new int[5];
				int ans = 0;
				Console.WriteLine("請輸入五位同學的成績:");
				for (int i = 0; i < arr.Length; i++)
				{
					arr[i] = int.Parse(Console.ReadLine());
					ans += arr[i];		
				}
				Console.WriteLine("五位同學的總成績爲:"+ans);
				Console.WriteLine("五位同學的平均成績:"+ans/5);
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.Message.ToString());
			}
			Console.ReadKey();
        }
    }
}

小劇場:我相信我將能看到彩虹的美麗。

英文版:I believe I will be able to see the beauty of the rainbow.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章