c#问题,望高手指点加30

来源:百度知道 编辑:UC知道 时间:2024/05/10 14:42:59
using System;
public class Test
{
public static void Main()
{

int[] a=new int[]{67,87,4,9,56,98,24,89,43,1};
int i,t=0,temp=100;
for(i=0;i<a.Length;i++)
{
if(a[i]<=temp)

{

temp=a[i];
t=i;
}

}

Console.WriteLine(temp);
Console.WriteLine(t);
}

}

句子中的
temp=a[i];
t=i;
是什么意思

为什么开始的时候temp要赋值100

为什么开始的时候temp要赋值100?
a[]里面最大的是98,赋值100是确保可以加入if里面

如果a[i]里面的值小于等于当前的temp
则把a[i]给temp,把i给t

但是最后WrintLine只是输出最后一次的值

这段代码一点意义都没有,
想问你哪里来的

这里看起来是要求67,87,4,9,56,98,24,89,43,1 中最小的数
并输出它在数组中的索引
temp=a[i];
当a[i]< temp时
是把数组中索引为i的值赋给temp
t=i;
记录下当前数组的索引
temp肤质100是随机的只要比数组中的数都大就行

if(a[i]<=temp)

{

temp=a[i];
t=i;
}

}
如果a[i]<=temp
则用temp存储a[i]的值.用t存储这个数"a[i]"在数组中的下标

for (int i=1 ;i<a.Length-1;i++)
for (int j=a.Length-1;j>=i;j--)
{
int temp;
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}

冒泡排序 输入a[0]就是最小的了