這個沒問題,但是為什么我把student改成name就報錯?
????????????// 所有學生姓名數組
??????????? string[] names = {"景珍","林惠洋","成蓉","洪南昌","龍玉民","單江開","田武山","王三明" };
??????????? // 所有學生分數數組
??????????? int[] scores = new int[]{90,65,88,70,46,81,100,68};
?????????? ?
??????????? // 總分 平均分
??????????? int sum = 0, avg;
?????????? ?
??????????? // 遍歷數組求學生總分
??????????? foreach (int i in scores) {
??????????????? sum += i;
??????????? }
?????????? ?
??????????? // 通過總分獲得平均分數
??????????? avg = sum/scores.Length;
?????????? ?
??????????? // 高于平均分的學生姓名集
??????????? string student = null;
??????????? for (int i = 0; i < scores.Length; i++) {
??????????????? // 比較學生分數和平均分
??????????????? if (scores[i] > avg) {
??????????????????? // 將高于平均分的學生姓名加入姓名集
??????????????????? student = student + " " + names[i] + " ";
??????????????? }
??????????? }
?????????? ?
??????????? // 打印結果
??????????? Console.WriteLine("平均分是" + avg + ",高于平均分的有:");
??????????? Console.WriteLine(student);
2019-09-06
這個本來就沒有錯呀,可能你自己再操作時,編寫錯誤了誤把name寫成names吧,我拷貝你的代碼再程序中運行,然后換成name還是同樣的結果,沒有錯啊,正確的,只是換個名稱而已,且name又不是關鍵字,所以是可以的。
2019-08-30
我改了沒問題, 檢查一下你的拼寫;
```
using System;
using System.Collections.Generic;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//請在這里完善代碼
// 所有學生姓名數組
string[] names = { "景珍", "林惠洋", "成蓉", "洪南昌", "龍玉民", "單江開", "田武山", "王三明" };
// 所有學生分數數組
int[] scores = new int[] { 90, 65, 88, 70, 46, 81, 100, 68 };
// 總分 平均分
int sum = 0, avg;
// 遍歷數組求學生總分
foreach (int i in scores)
{
sum += i;
}
// 通過總分獲得平均分數
avg = sum / scores.Length;
// 高于平均分的學生姓名集
string name = null;
for (int i = 0; i < scores.Length; i++)
{
// 比較學生分數和平均分
if (scores[i] > avg)
{
// 將高于平均分的學生姓名加入姓名集
name = name + " " + names[i] + " ";
}
}
// 打印結果
Console.WriteLine("平均分是" + avg + ",高于平均分的有:");
Console.WriteLine(name);
}
}
}
```