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