為什么跑出來結果不一樣呢?
using System;
using System.Collections.Generic;
using System.Text;
namespace projGetMaxScore
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? string[] name = new string[]{"吳松","錢東宇","伏晨","陳陸","周蕊","林日鵬","何昆","關欣"};
? ? ? ? ? ? int[] score = new int[]{89,90,98,56,60,91,93,85};
? ? ? ? ? ? int max,i,j;
? ? ? ? ? ? for(i=1,j=1;i<score.Length;i++){
? ? ? ? ? ? ? ? max=score[0];
? ? ? ? ? ? ? ? if(max<score[i]){
? ? ? ? ? ? ? ? ? ? max=score[i];
? ? ? ? ? ? ? ? ? ? j=i;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine("分數最高的是{0},分數是{1}",name[j],score[j]);
? ? ? ? }
? ? }
}
結果:分數最高的是何昆,分數是93
2019-02-25
你把max=score[0];放到For 循環的外面,不然每進行一次循環,都會重新賦值.
2019-01-30
你這個每次循環都是先把score[0]的值賦給max變量,然后再進行if條件選擇,所以循環執行到倒數第二個數據的時候,89<93成立,93對應的i被賦值給j 。而循環到最后一個,85<93不成立,就不交換,所以結果就是93
2019-01-16
你把max=score[0]放到循環外面就行了