我想問一下大家,我這個“最高分者”跟“高于平均分的人”都不對,我找不到原因,有人幫我看一下嗎?謝謝你們
?? 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();??????? }??? }}
2017-09-26
看錯了 以為你寫的是最后一個題呢 我直接把我編的發出來了? 我看了一下你那個? 可以實現你要的功能啊
2017-09-26
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);
??????? }
??? }
}