使用雙指針法
string[] names = new string[]{"吳松", "錢東宇", "伏晨", "陳陸", "周蕊", "林日鵬", "何昆", "關欣"};
int[] scores = new int[]{89, 90, 98, 56, 60, 91, 93, 85};
int index=0;? //?定義一個外部指針,用來接收最大值的索引值
for (int i=0;i<scores.Length;i++)
{
????if (scores[index]<scores[i]){? // 將大的索引值賦值給index
????????index = i;
????}
}
// 循環運行完畢,index就是最大值的索引,然后利用字符串格式化輸出
Console.WriteLine("分數最高的是{0},分數是{1}",names[index], scores[index]);
2021-08-16
外部指針的作用是記錄最大值的索引
內部指針的作用是遍歷數組,也就是那個臨時變量i