Question

Hello! I am coding in c# and I am having trouble using boolean variables and if...

Hello!

I am coding in c# and I am having trouble using boolean variables and if statements. The project I am working on is based on the users input, and the code should branch into two different options. However, I am struggling to understand how to take the users input to decide which path to take. Another problem I am having is with my if statements. After the user has inputed information and the code decides to take them down path 1 or path 2, it does not use the information correctly. Here is the bit of code I am talking about:

if (Hourly >= 100.00)
{
TotalPay = Hourly - (Hourly * 0.10);
FinalPay = TotalPay * 0.10;
NextPay = TotalPay - FinalPay;
}
else
{
TotalPay = Hourly;
FinalPay = TotalPay;
NextPay = TotalPay;
}
if (Hourly >= 50.00)
{
TotalPay = Hourly - (Hourly * 0.02);
FinalPay = TotalPay * 0.10;
NextPay = TotalPay - FinalPay;
}
if (Hourly < 100.00)
{
TotalPay = Hourly - (Hourly * 0.02);
FinalPay = TotalPay * 0.10;
NextPay = TotalPay - FinalPay;
}
if (Hourly < 50.00)
{
TotalPay = Hourly - (Hourly * 0.05);
FinalPay = TotalPay * 0.10;
NextPay = TotalPay - FinalPay;
}

Console.WriteLine(NextPay);

It keeps using the wrong if statement and I am not sure why. I have found the source of it, which is in the statement if (Hourly >=50.00). I am not for sure why it keeps overriding the if (Hourly >= 100.00) . I do know that it is technically correct in running because it is greater then or equal to 50, but I would think it would take precedent and use the 100.00. Any help would be appreciated!

Homework Answers

Answer #1

Stacking 'if' one over the other will result in this problem.

After one 'if', use 'else' if in the next if. I also used to make the same mistake. Here, I have made the correction. And else should be in the end always. Also, you can combine boolean expressions which have the same code. Try to keep the if conditions in order. Look athe modified code, you will understand.

if (Hourly < 50.00)
{
TotalPay = Hourly - (Hourly * 0.05);
FinalPay = TotalPay * 0.10;
NextPay = TotalPay - FinalPay;
}
else if (Hourly > 50.00 && Hourly < 100.00)
{
TotalPay = Hourly - (Hourly * 0.02);
FinalPay = TotalPay * 0.10;
NextPay = TotalPay - FinalPay;
}
if (Hourly >= 100.00)
{
TotalPay = Hourly - (Hourly * 0.10);
FinalPay = TotalPay * 0.10;
NextPay = TotalPay - FinalPay;
}

else
{
TotalPay = Hourly;
FinalPay = TotalPay;
NextPay = TotalPay;
}
Console.WriteLine(NextPay);
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hello, I am having trouble with this question. I am using the HP 10bii+ calculator. A...
Hello, I am having trouble with this question. I am using the HP 10bii+ calculator. A step by step instructions on using the calculator, not excel, will help me the most. I know the answer, just not how to enter it into the calculator to get the correct answer, i keep getting it wrong. Thank you! What is the IRR of the following set of cash flows? (Do not round intermediate calculations. Enter your answer as a percent rounded to...
Hello, I am having trouble with this question. I am using the HP 10bii+ calculator. A...
Hello, I am having trouble with this question. I am using the HP 10bii+ calculator. A step by step instructions on using the calculator, not excel, will help me the most. I know the answer, just not how to enter it into the calculator to get the correct answer, i keep getting it wrong. Thank you! A project that provides annual cash flows of $17,100 for nine years costs $77,000 today.    What is the NPV for the project if...
I am trying to solve the following C++ problem over structures. I am having trouble with...
I am trying to solve the following C++ problem over structures. I am having trouble with the very last structure and I made a comment next to where I am confused. I included the assignment instructions and my current code. I would appreciate if anyone can help me explain what I need to do for the last structure. Thank you. Assignment Instructions Create a program that will prompt me for each of the following items: 1. Date of Birth 2....
Hello, This is a multiple choice homework study question that I am having difficulty with. Although...
Hello, This is a multiple choice homework study question that I am having difficulty with. Although we have been told there is only one answer, I can see where all answers could be correct on some level but we have to determine the PRIORITY question a patient would be asked. My initial reaction would be answer C because longevity would indicate the severity of mental illness, I am really not sure Question: A patient with schizophrenia, who admits to auditory...
I am having trouble visualizing/drawing this graph so that I can answer this question. --------------- Consider...
I am having trouble visualizing/drawing this graph so that I can answer this question. --------------- Consider the market for hamburgers. Suppose that, in a competitive market without government regulations, the equilibrium price of hamburgers is $7 each, and employees at fast-food restaurants earn $19.50 per hour. Complete the following table by indicating whether each of the statements is an example of a price ceiling or a price floor and whether it results in a shortage or a surplus or has...
Binary Search Tree with multiple structs? Hi, I am having an issue with trying to create...
Binary Search Tree with multiple structs? Hi, I am having an issue with trying to create a binary search tree while having multiple structs. The struct code provided is provided for us. #define CAT_NAME_LEN 25 #define APP_NAME_LEN 50 #define VERSION_LEN 10 #define UNIT_SIZE 3 struct app_info{ char category[CAT_NAME_LEN]; // name of category char app_name[APP_NAME_LEN]; // name of application char version[VERSION_LEN]; // version number float size; // size of application char units[UNIT_SIZE]; // GB or MB float price; // price in...
*Note: 4 of the 5 questions are correct. I am having great trouble figuring out the...
*Note: 4 of the 5 questions are correct. I am having great trouble figuring out the 4th part of the question. What I have listed below is the problem in its entirety. Bob's Bumpers has a repetitive manufacturing facility in Kentucky that makes automobile bumpers and other auto body parts. The facility operates 280 days per year and has annual demand of 66,000 bumpers. They can produce up to 425 bumpers each day. It costs $61 to set up the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT