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! 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....
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...
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...