日期:2014-05-17 浏览次数:20429 次
int num1, num2; //前后两个数 num1 = num2 = 0; int c = 101; int[] i = { 1, 8, 9, 11, 12, 19, 33, 100, 101, 200 }; for (int k = 0; k < i.Length; k++) { if (i[k] == c) { num1 = i[k - 1 < 0 ? k : k - 1]; num2 = i[k + 1 == i.Length ? k : k + 1]; break; } }
------解决方案--------------------
int num1=0;
int num2=0;
int c = 100;
int[] i = { 1, 8, 9, 11, 12, 19, 33, 100, 101, 200 };
int len=i.Length;//我都叫我手下这么定义一个值,不然循环里面如果数量大的情况会变慢
for (int k = 0; k < len; k++)
{
if (i[k] == c)
{
num1 = i[k - 1];
if((k + 1)>len)
{
num2 =0;
}else{
num2=i[k +1];
}
break;
}
}