C #, If Statement, Min and Max
Good evening; I am writing some code that solves the following equation.
X - device size Y - device quantity A - denominator Z - total diversified cost
(X * Y) / A = Z
Here is the part I don't know how to accomplish. The value of A is determined by the value of Y. If Y is between 3 and 6 than A = .7, if Y is between 6 and 9 than A = .6; etc.
Which function should I use to achieve the above? Any help is appreciated.
Hello,
Greg Rutledge
source to share
There are 3 approaches to this: IMO:
1) Calculation of the formula. So you want to know that A is given to Y if you have enough data, for example. taking your .7 for 3 <= Y <6, .6 for 6
A = .8-(Y/3)/10.0;
You may need to cast or truncate the Y / 3 part if Y is not a multiple of 3, or you can do this to extract the fractional part: (Y- (Y% 3)) / 3
2) Use a while loop structure to take 3 out of Y, note that the statements in while are abbreviated, which can make this somewhat obscure:
int Holder = Y, A=.8;
while (Holder > 0)
{
A-= .1;
Holder-= 3;
}
3) If / elseif. If Y is bounded, then you can use a brute force assignment strategy:
If Y<3
A=.8
Else if Y < 6
A=.7
Else if Y < 9
A=.6
and etc.
This is in the order that I would like to consider to address such a problem.
source to share
Ok, taking the code you just posted, I think this is what you are 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();
source to share
If the values ββare predefined in the list and not created using a function.
Basically every object has a minValue, maxValue, value, minLink and maxLink.
Follow the links until you find the target value or null pointer.
if Y <= maxValue then
if Y >= minValue then
return value
else
follow minLink
else
follow maxLink
source to share