為什么跑出來結(jié)果不一樣呢?
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("分數(shù)最高的是{0},分數(shù)是{1}",name[j],score[j]);
? ? ? ? }
? ? }
}
結(jié)果:分數(shù)最高的是何昆,分數(shù)是93
2019-02-25
你把max=score[0];放到For 循環(huán)的外面,不然每進行一次循環(huán),都會重新賦值.
2019-01-30
你這個每次循環(huán)都是先把score[0]的值賦給max變量,然后再進行if條件選擇,所以循環(huán)執(zhí)行到倒數(shù)第二個數(shù)據(jù)的時候,89<93成立,93對應的i被賦值給j 。而循環(huán)到最后一個,85<93不成立,就不交換,所以結(jié)果就是93
2019-01-16
你把max=score[0]放到循環(huán)外面就行了