|
-
April 16th, 2012, 04:17 AM
#1
Problems with my if else statement
Using net4.0 visual studio 2010
Hey guys I am designing a code as practice that asks the user to input the number of packages they bought and based on how many it will give them a discount and show them the total discounted price and also how much they got discounted.
For example if they bought between 10 and 19 packages they would get a 20% discount.
This is what I have written so far:
Code:
private void calculateButton_Click(object sender, EventArgs e)
{
const int SET_PRICE = 99;
int quantity;
double totalPrice;
double discountPrice;
double discountAmount;
quantity = int.Parse(quantityTextBox.Text);
if (quantity <= 19 || quantity > 10)
{
totalPrice = quantity * SET_PRICE;
discountPrice = totalPrice * 0.80;
discountAmount = totalPrice * 0.20;
discountLabel.Text = discountPrice.ToString("c");
discountAmountLabel.Text = discountAmount.ToString("c");
}
else if (quantity <= 49 || quantity >= 20)
{
totalPrice = quantity * SET_PRICE;
discountPrice = totalPrice * 0.70;
discountAmount = totalPrice * 0.30;
discountLabel.Text = discountPrice.ToString("c");
discountAmountLabel.Text = discountAmount.ToString("c");
Every time I type a quantity over 19 (say i type 20) it gives me the completely wrong discount every single time. From what I can see it is completely ignoring my second if else statement and still calculating it with the percentages from my first if statement. Can anyone point me in the wrong direction here? Why is it skipping my if else statement and still using my if statement even if I specified over 19 packages.
Last edited by DataMiser; April 16th, 2012 at 05:08 PM.
Reason: added code tags
-
April 16th, 2012, 12:27 PM
#2
Re: Problems with my if else statement
if (quantity <= 19 || quantity > 10)
A quantity of 20 will obviously satisfy 'quantity > 10'. Do you want to use '&&' instead of '||' in these statements?
-
April 17th, 2012, 11:44 AM
#3
Re: Problems with my if else statement
Bit off topic but have you tried the TryParse method for converting the value of your textbox to an integer? Means you wouldn't have to deal with an exception.
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
On topic, looks like "Zip" nailed it on the head.
Just another comment, when you say between 10 and 19 do you mean 10 and 19 are inclusive? You'll need to change it to >= 10 if you do, else you will ignore the 10th package.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|