there is a logical error in my if statement
if (quantity %26lt; 20)
{
unitPrice = 20;
if (quantity %26lt; 50)
{
unitPrice = 15;
}
if (quantity %26lt; 100)
{
unitPrice = 12;
}
if (quantity %26lt; 500)
{
unitPrice = 8;
}
}
else
{
unitPrice = 5;
}
Visual C# question?
The corrected code.
if (quantity %26lt; 20)
{
unitPrice = 20;
}
else if (quantity %26lt; 50)
{
unitPrice = 15;
}
else if (quantity %26lt; 100)
{
unitPrice = 12;
}
else if (quantity %26lt; 500)
{
unitPrice = 8;
}
else
{
unitPrice = 5;
}
Reply:In your example unitPrice will always end up as 8.
Why don't you use the switch statement?
switch (quantity)
{
case %26lt; 20:
unitPrice = 20;
break;
case %26lt; 50:
unitPrice = 15;
break;
case %26lt; 100:
unitPrice = 12;
break;
case %26lt; 500:
unitPrice = 8;
break;
default:
unitPrice = 5;
break;
}
Edit: OK - you can't do this, you can only use constants in the case statement. Sorry, you will have to use the if-then statement, but in reverse.
unitPrice = 5;
if (if (quantity %26lt; 500) {
unitPrice = 8;
}
if (quantity %26lt; 100) {
unitPrice = 12;
}
if (quantity %26lt; 50) {
unitPrice = 15;
}
if (quantity %26lt; 20) {
unitPrice = 20;
}
Reply:yes:
I am not confident with my VC# syntax, but your problem lies in your if statement.
Basically you are currently saying:
if x %26lt; 20 then ... if the same x %26lt; 50 then ...
Try something like this:
unitPrice = 5;
if (quantity %26lt; 20)
{ unitPrice = 20;}
if (quantity %26lt; 50)
{ unitPrice = 15; }
if (quantity %26lt; 100)
{ unitPrice = 12;}
if (quantity %26lt; 500)
{ unitPrice = 8;}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment