随机产生20个10~99之间的整数,要求:1)用冒泡法排序(降序)

2021-12-16 教育 202阅读
inti,j;
int[]number=newint[20];
privatevoidForm1_Load(objectsender,EventArgse)
{
intx;
stringstr="";
Randomr=newRandom();
for(i=0;i<20;i++)
{
x=r.Next(1,99);
number[i]=x;
str=str+number[i].ToString()+"";
}
tBoxInput.Text=str.Trim();
}
privatevoidbtnOK_Click(objectsender,EventArgse)
{
inttemp;
stringstr="";
for(i=0;i<19;i++)
{
for(j=0;j<19-i;j++)
{
if(number[j]>number[j+1])
{
temp=number[j];
number[j]=number[j+1];
number[j+1]=temp;
}
}
}
for(i=0;i<20;i++)
{
str+=number[i].ToString()+"";
}
tBoxOutput.Text=str.Trim();
}
privatevoidbtnM_Click(objectsender,EventArgse)
{
if(tBoxOutput.Text=="")
{
MessageBox.Show("请先排序!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else
{
tBoxMax.Text=tBoxOutput.Text.Substring(tBoxOutput.Text.Length-2,2);
tBoxMin.Text=tBoxOutput.Text.Substring(0,2);
}
}
privatevoidbtnAverage_Click(objectsender,EventArgse)
{
doublesum1=0,sum2=0;
doublecount1=0,count2=0;
doubleaverage1=0,average2=0;
for(i=0;i<20;i++)
{
if(number[i]%2!=0)
{
count1++;
sum1+=number[i];
average1=sum1/count1;
tBoxA_Jishu.Text=average1.ToString();
}
else
{
count2++;
sum2+=number[i];
average2=sum2/count2;
tBoxA_Oushu.Text=average2.ToString();
}
}
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com