赞
踩
实验目的及要求
Microsoft Visual Studio 2008
注意考虑以下情况:
①输入成绩在 0-100 的范围之外的处理。
②如果输入的成绩后面带有分号等标点符号,也可以过滤。
③当有多个相同的最高或最低分时,也能分别显示出来。
实验的程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实验三_1_ { class Program { static double Average(int[] Scores) { double total = 0; for (int i = 0; i < Scores.GetLength(0); i++) total += Scores[i]; return (double)(total / Scores.GetLength(0)); } static void Main(string[] args) { int[] Scores = new int[10]; for (int i = 0; i < Scores.Length; i++) { int a; Console.Write("请输入第{0}个学生成绩 :", i); a = int.Parse(Console.ReadLine()); Scores[i] = a; } int a1 = 0; int high = 0; for (int i = 0; i < 10; i++) { if (Scores[i] > high) { high = Scores[i]; a1 = i; } } int a2 = 0; int low =100; for (int j = 0; j < 10; j++) { if (Scores[j] < low) { low = Scores[j]; a2 = j; } } Console.WriteLine("平均成绩: {0}", Average(Scores)); Console.WriteLine("最高成绩: {0},下标是: {1}",high ,a1); Console.WriteLine("最低成绩: {0},下标是: {1}",low ,a2); Console.ReadLine(); } } }
注意考虑以下情况:
a = int.Parse(Console.ReadLine());
if (a < 0 || a > 100)
{
Console.WriteLine("输入错误");
a = a - 1;
}
else
Scores[i] = a;
a = Console.ReadLine();
a = Regex.Replace(a,@"[^\d]*", "");
使用Regex.Replace将其标点转换成字符。
在用int.Parse转换为整形数字。
int high = 0;
for (int i = 0; i < 10; i++)
{
If (Scores[i] > high)
high = Scores[i];
}
Console.Write("最高成绩为{0},下标是", high);
for (i = 0; i < 10; i++)
{
if (a[i] == high)
Console.Write(",{0}", high +1);
}
实验的运行效果如下:
实验的程序如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 实验三_2_ { class Program { static void Main(string[] args) { double[] aver1 = new double[5]; double [] aver2=new double [4]; int[,] arr = new int[4, 5]; for (int i = 0; i < 4; i++) for (int j = 0; j < 5; j++) arr[i, j] = (i + 1) * (j + 1); for (int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) Console.Write("{0}\t", arr[i, j]); Console.WriteLine(); } for (int i = 0; i < 4; i++) { aver2[i] = 0; for (int j = 0; j < 5; j++) { aver2[i] += arr[i, j]; } aver2[i] /= 5 * 1.0; } for (int i = 0; i < 4; i++) { Console.WriteLine("第" + (i + 1) + "行的平均值为: {0}", aver2[i]); } Console.WriteLine(); for (int i = 0; i < 5; i++) { aver1[i] = 0; for (int j = 0; j < 4; j++) { aver1[i] += arr[j, i]; } aver1[i] /= 4 * 1.0; } for (int i = 0; i < 5; i++) { Console.WriteLine("第" + (i + 1) + "列的平均值为: {0}", aver1[i]); } Console.ReadLine(); } } }
实验的运行效果如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。