C#,If Statement,Min和Max

时间:2021-02-12 20:14:50

Good evening; I am writing a bit of code that solves the following equation.

晚上好;我正在编写一些代码来解决以下等式。

X is size of device Y is quantity of device A is the denominator Z is the total diversified value

X是设备Y的大小,设备A的数量是分母Z是总的多样化值

(X * Y)/A = Z

(X * Y)/ A = Z.

Here is the part I don't know how to accomplish. The value of A is found by the amount of Y. If Y is between 3 and 6 than A = .7, if Y is between 6 and 9 than A = .6; and so on.

这是我不知道如何完成的部分。 A的值由Y的量得出。如果Y在3和6之间而不是A = .7,如果Y在6和9之间而不是A = .6;等等。

What function should I use to accomplish the above? Any help is greatly appreciated.

我应该用什么功能来实现上述目标?任何帮助是极大的赞赏。

Regards,

Greg Rutledge

5 个解决方案

#1


There are 3 approaches to this, IMO:

有三种方法,IMO:

1) Formula calculation. Thus, you want to know what A is given Y which if you have enough data, e.g. taking your .7 for 3<=Y<6, .6 for 6

1)公式计算。因此,如果您有足够的数据,例如,您想知道A给出的是什么。以.7表示3 <= Y <6,.6表示6

A = .8-(Y/3)/10.0;

You may need to use a cast or truncate function on the Y/3 part if Y isn't a multiple of 3, or you could do this to take out the fractional part: (Y-(Y % 3))/3

如果Y不是3的倍数,您可能需要在Y / 3部分使用强制转换或截断函数,或者您可以执行此操作以取出小数部分:(Y-(Y%3))/ 3

2) Use a while loop structure to take out the 3's of Y, note that the statements in the while are abbreviated which may make it a bit unclear:

2)使用while循环结构取出3的Y,注意while中的语句是缩写的,这可能会使它有点不清楚:

int Holder = Y, A=.8;
while (Holder > 0)
{
    A-= .1;
    Holder-= 3;
}

3) If/elseif. If Y is bounded then there is a brute force assignment strategy you could use:

3)如果/ elseif。如果Y有界,那么您可以使用强力分配策略:

 If Y<3 
    A=.8
 Else if Y < 6 
    A=.7
 Else if Y < 9 
    A=.6

etc.

This is in order of what I'd consider for solving such a problem.

这是我考虑解决这个问题的顺序。

#2


You can use if and the comparison operators (<=). Homework?

您可以使用if和比较运算符(<=)。家庭作业?

What do you mean by "and so on"? If the cases are regular then maybe you can use a formula instead of a bunch of if statements.

“依此类推”是什么意思?如果案例是常规的,那么也许你可以使用公式而不是一堆if语句。

if(Y <= 3.0)      A = ...; 
else if(Y <= 6.0) A = 0.7; 
else if(Y <= 9.0) A = 0.6;
...

#3


Assuming A starts at 0.8 and decreases by 0.1 with every increase by 3 of Y:

假设A从0.8开始,每增加3,Y减少0.1:

int temp = Y / 3;
float A = 0.8f - (temp / 10f);

#4


Ok, taking the code that you just posted, I think this is what you're looking for:

好的,拿你刚刚发布的代码,我想这就是你要找的东西:

if ((cb5_1.Checked)&&(cb5_2.Checked)&&(cb5_3.Checked))
{
    //if the first three text boxes are checked calculate based on the following. 
    decimal a, b, c, d, z;
    decimal aa, bb, cc, zz;
    a = decimal.Parse(cbx5_1a.Text);
    b = decimal.Parse(cbx5_2a.Text);
    c = decimal.Parse(cbx5_3a.Text);
    aa = decimal.Parse(cbx5_1q.Text);
    bb = decimal.Parse(cbx5_2q.Text);
    cc = decimal.Parse(cbx5_3q.Text);
    z = (aa+bb+cc);

    d = 0.8m - ((z / 3) / 10m);

    zz = ((a*aa)+(b*bb)+(c*cc))*d;

    tb5_atotal.Text = Math.Round(z,2).ToString();

#5


If the values are predefined in a list, and not created with a function.

如果值是在列表中预定义的,而不是使用函数创建的。

Basically each object has a minValue, maxValue, value, minLink and maxLink.

基本上每个对象都有一个minValue,maxValue,value,minLink和maxLink。

Follow links until you find the target value or a null pointer.

按照链接,直到找到目标值或空指针。

if Y <= maxValue then 
    if Y >= minValue then
        return value
    else
        follow minLink
else 
    follow maxLink

#1


There are 3 approaches to this, IMO:

有三种方法,IMO:

1) Formula calculation. Thus, you want to know what A is given Y which if you have enough data, e.g. taking your .7 for 3<=Y<6, .6 for 6

1)公式计算。因此,如果您有足够的数据,例如,您想知道A给出的是什么。以.7表示3 <= Y <6,.6表示6

A = .8-(Y/3)/10.0;

You may need to use a cast or truncate function on the Y/3 part if Y isn't a multiple of 3, or you could do this to take out the fractional part: (Y-(Y % 3))/3

如果Y不是3的倍数,您可能需要在Y / 3部分使用强制转换或截断函数,或者您可以执行此操作以取出小数部分:(Y-(Y%3))/ 3

2) Use a while loop structure to take out the 3's of Y, note that the statements in the while are abbreviated which may make it a bit unclear:

2)使用while循环结构取出3的Y,注意while中的语句是缩写的,这可能会使它有点不清楚:

int Holder = Y, A=.8;
while (Holder > 0)
{
    A-= .1;
    Holder-= 3;
}

3) If/elseif. If Y is bounded then there is a brute force assignment strategy you could use:

3)如果/ elseif。如果Y有界,那么您可以使用强力分配策略:

 If Y<3 
    A=.8
 Else if Y < 6 
    A=.7
 Else if Y < 9 
    A=.6

etc.

This is in order of what I'd consider for solving such a problem.

这是我考虑解决这个问题的顺序。

#2


You can use if and the comparison operators (<=). Homework?

您可以使用if和比较运算符(<=)。家庭作业?

What do you mean by "and so on"? If the cases are regular then maybe you can use a formula instead of a bunch of if statements.

“依此类推”是什么意思?如果案例是常规的,那么也许你可以使用公式而不是一堆if语句。

if(Y <= 3.0)      A = ...; 
else if(Y <= 6.0) A = 0.7; 
else if(Y <= 9.0) A = 0.6;
...

#3


Assuming A starts at 0.8 and decreases by 0.1 with every increase by 3 of Y:

假设A从0.8开始,每增加3,Y减少0.1:

int temp = Y / 3;
float A = 0.8f - (temp / 10f);

#4


Ok, taking the code that you just posted, I think this is what you're looking for:

好的,拿你刚刚发布的代码,我想这就是你要找的东西:

if ((cb5_1.Checked)&&(cb5_2.Checked)&&(cb5_3.Checked))
{
    //if the first three text boxes are checked calculate based on the following. 
    decimal a, b, c, d, z;
    decimal aa, bb, cc, zz;
    a = decimal.Parse(cbx5_1a.Text);
    b = decimal.Parse(cbx5_2a.Text);
    c = decimal.Parse(cbx5_3a.Text);
    aa = decimal.Parse(cbx5_1q.Text);
    bb = decimal.Parse(cbx5_2q.Text);
    cc = decimal.Parse(cbx5_3q.Text);
    z = (aa+bb+cc);

    d = 0.8m - ((z / 3) / 10m);

    zz = ((a*aa)+(b*bb)+(c*cc))*d;

    tb5_atotal.Text = Math.Round(z,2).ToString();

#5


If the values are predefined in a list, and not created with a function.

如果值是在列表中预定义的,而不是使用函数创建的。

Basically each object has a minValue, maxValue, value, minLink and maxLink.

基本上每个对象都有一个minValue,maxValue,value,minLink和maxLink。

Follow links until you find the target value or a null pointer.

按照链接,直到找到目标值或空指针。

if Y <= maxValue then 
    if Y >= minValue then
        return value
    else
        follow minLink
else 
    follow maxLink