using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace bubble_sort
{
///<summary>
///本程序使用二重循环实现冒泡排序
///</summary>
class Program
{
static void Main(string[] args)
{
int[] score = new int[5]; //票数数组
int i, j; //循环变量
int temp; //临时变量 //读入分数
Console.WriteLine("请输入5个人的成绩");
for (i = 0; i < 5; i++)
{
Console.WriteLine("请输入第{0}各学院的成绩:", i + 1);
score[i] = int.Parse(Console.ReadLine()); //类型转换
} //开始排序
for (j = 0; j < score.Length - 1; i++) //控制比较多少轮
{
//将最大的元素交换到最后
for (j = 0; j < score.Length - 1 - i; j++)
{
if (score[j] > score[j + 1])
{
//交换元素
temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
}
//排列后输出
Console.WriteLine("排列后的成绩为:");
for (i = 0; i < score.Length; i++)
{
Console.Write("{0}\t", score[i]);
}
Console.ReadLine();
}
}
}运行后 if (score[j] > score[j + 1]) 这里提示数组越界?哪里出错了?
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace bubble_sort
{
///<summary>
///本程序使用二重循环实现冒泡排序
///</summary>
class Program
{
static void Main(string[] args)
{
int[] score = new int[5]; //票数数组
int i, j; //循环变量
int temp; //临时变量 //读入分数
Console.WriteLine("请输入5个人的成绩");
for (i = 0; i < 5; i++)
{
Console.WriteLine("请输入第{0}各学院的成绩:", i + 1);
score[i] = int.Parse(Console.ReadLine()); //类型转换
} //开始排序
for (j = 0; j < score.Length - 1; i++) //控制比较多少轮
{
//将最大的元素交换到最后
for (j = 0; j < score.Length - 1 - i; j++)
{
if (score[j] > score[j + 1])
{
//交换元素
temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
}
//排列后输出
Console.WriteLine("排列后的成绩为:");
for (i = 0; i < score.Length; i++)
{
Console.Write("{0}\t", score[i]);
}
Console.ReadLine();
}
}
}运行后 if (score[j] > score[j + 1]) 这里提示数组越界?哪里出错了?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货