注册 登录
编程论坛 C# 论坛

c#for..if语句

书虫zz 发布于 2016-12-27 15:54, 1717 次点击
float[] c = new float[18];
        float[]c1 = new float[18];
        double a,b,q,x,x1,q1;
        float d1, d2, d3, o,o1 ;
        int p,p1;
        d1 = float.Parse(GZYL.SelectedValue);
        d2 = float.Parse(LLTL.SelectedValue);
        d3 = float.Parse(SB.SelectedValue);
        a = Math.Sqrt(d2/d1);
        a = 35.7 * a;
        b = Math.Sqrt((d3 - 1) / d3);
        b = a * b;
        GJ.Text = a.ToString("f2");
        HSGJ.Text = b.ToString("f2");
 o = float.Parse(GJ.Text);
        c[0] = 25;
        c[1] = 32;
        c[2] = 40;
        c[3] = 50;
        c[4] = 63;
        c[5] = 80;
        c[6] = 100;
        c[7] = 125;
        c[8] = 160;
        c[9] = 200;
        c[10] = 220;
        c[11] = 250;
        c[12] = 280;
        c[13] = 320;
        c[14] = 360;
        c[15] = 400;
        c[16] = 450;
        c[17] = 500;
        q = Math.Abs(o - c[0]);
        for (int i = 0; i < 18; i++)
        {
            p = 0;
            c[i] = Math.Abs(o - c[i]);
            if (c[i] >= q)
            {
                q = q;
            }
            else
                q = c[i];
                p = i;        
        }
        x = c[p];
        GJ.Text = x.ToString("f2");
想最后提取出来这个i,使x=c[p]的值,但是x = c[p];标红,显示p未赋值,放进for语句里面没有问题,但是我就是想通过这个语句找到圆整的值的,大神们帮忙看看应该怎么写?
1 回复
#2
zbjzbj2016-12-28 11:43
           q = Math.Abs(o - c[0]);
            for (p = 0; p < 18; p++)
            {
                c[i] = Math.Abs(o - c[p]);
                if (c[p] < q)
                    q = c[p];
            }
            x = q;
            GJ.Text = x.ToString("f2");
        }
    }

[此贴子已经被作者于2016-12-28 11:48编辑过]

1