慕仙8563930
2017-09-25 22:27
static void Main(string[] args) { string[] name = new string[2]; int[] score = new int[2]; for (int i=0;i<name.Length;i++) { Console.WriteLine("输入第" + (i + 1) + "位的姓名:"); name[i] = Console.ReadLine(); Console.WriteLine("输入第" + (i + 1) + "位的分数:"); score[i] = int.Parse(Console.ReadLine()); } int sum=0,avg; for (int i=0;i<score.Length;i++) { sum += score[i]; } avg = sum / score.Length; Console.WriteLine("总分数:{0}平均分:{1}",sum,avg); int max=0; string topone=null; for (int j=1;j<score.Length;j++) { if (score[j] > max) max = score[j]; topone = name[j]; } Console.WriteLine("最高分是{0}", max); Console.WriteLine("最高分者是{0}",topone); int avgp=score[0]; string avgpp =name[0]; for (int k=1;k<score.Length;k++) { if (score[k] > avg) avgp = score[k]; avgpp = name[k]; } Console.WriteLine("高于平均分的人有{0}",avgpp); Console.ReadLine(); } }}
看错了 以为你写的是最后一个题呢 我直接把我编的发出来了 我看了一下你那个 可以实现你要的功能啊
using System;
using System.Collections.Generic;
using System.Text;
namespace projAboveAvg
{
class Program
{
static int Avg(int[] scores)
{
int avgscore=0;
int sum = 0;
foreach (var score in scores)
{
sum += score;
}
avgscore = sum / scores.Length;
return avgscore;
}
static void BeyondAvg(int[] scores,string[] names)
{
int i;
int avg = Avg(scores);
for (i = 0; i < scores.Length; i++)
{
if (scores[i]>avg)
{
Console.Write(names[i] + " ");
}
}
}
static void Main(string[] args)
{
int[] scores = { 90, 65, 88, 70, 46, 81, 100, 68 };
string[] names = { "景珍", "林惠洋", "成蓉", "洪南昌", "龙玉民", "单江开", "田武山", "王三明" };
Console.Write($"平均分是{Avg(scores)},");
Console.WriteLine("高于平均分的有:");
BeyondAvg(scores, names);
}
}
}
C#开发轻松入门
254118 学习 · 1459 问题
相似问题