简体   繁体   中英

Questions about Nested if Statements for Java

So the question my teacher wrote on the white board is: Input product Name, Quantity, price. Compute the total amount. If total amount is greater than 2000 discount is 5%, if total amount is greater than 5000 discount is 10%, if greater than 10000 discount is 20%

I know that you have to do this kind of format.

if (cost > 2000)
 discount = 0.05;

I would put in the correct things before than like

String prodName;
int qty, price;
double cost;
double discount;

I know what I put there is wrong but I think that's how the format of it goes before doing the nested if statement. I really need help on this. I don't need to be given the answers for this because that's spoon-feeding to the max level. I just need some pointers and a guide in order to find out what to do.

first declare all your variables like you did:

String prodName="";
int qty, price;
double cost; //your cost will be stored here
double discount =0;

Then do your calculations

if (cost > 2000 && cost <= 5000)
   discount = 0.05;
else if(cost > 5000 && cost <= 10000){
   discount = 0.1;
}else if(cost > 10000){
   discount = 0.2;
}

First of all you need to read your book thoroughly. Obviously this can be a good start . First learn how if-then works, next if-then-else and finally if-then-else if-then-else . Now lets analyze your question:

Input product Name, Quantity, price. Compute the total amount. If total amount is greater than 2000 discount is 5%, if total amount is greater than 5000 discount is 10%, if greater than 10000 discount is 20%.

Clearly you are going to set one variable, lets call it discount , with checking some condition. For explanation, say if the total amount is greater than 10000 , then you are going to set discount = 20% . But careful, when total amount is greater than 10000 , it also leads total amount is greater than 5000 or 2000 too! Then are you going to set discount = 10% or discount = 5% respectively? No, you are not.

Hence, to solve your problem, if a higher prioritize condition is already matched, you are not going to check any other conditions. So we will do the following:

discount;
total_amount = some Cost you calculated/inputted
if(total_amount > 10000) {
    discount = 0.2;
} else if(total_amount > 5000) {
    discount = 0.05;
} else if(total_amount > 2000) {
    discount = 0.01;
} else {
    discount = 0.00; // no discount for you coz total_amount less than or equal to 2000
}

Now what happens here? If the first test expression is true ( total_amount > 10000 ), it executes the code inside the braces { } just below it and no other block is executed. But if the first test expression is false , it checks the second test expression ( total_amount > 5000 ). If the second test expression is true , it executes the statement/s inside the braces { } just below it and no other block is executed. This process continues. If all the test expression are false , code/s inside else is executed and the control of program jumps below the if-else .

When you fully understand if-else then you can solve this problem in many different ways. Best of luck.

Just a few pointers, you are to compare the price, if you do it in reverse order you could do :

if (cost > 10000)
    discount = 0.2;
else if (cost > 5000)
    discount = 0.1;
else if (cost > 2000)
    discount = 0.05;

This is handy because you aren't nesting if statements and it still is correct.

if (cost > 10000)
    discount = 0.2;
else if(cost > 5000){
    discount = 0.1;
}else if(cost > 2000){
    discount = 0.05;
} else {
    discount = 0;
}
double total = cost - (cost * discount);

When using if and else ifs if the first if statement is true then that code is executed and none of the other else ifs are checked. If the first if is false then each else if following the if are checked in the order they appear. If one of them is true then that code is executed and none of the others are checked. However, if none are true and you had an else block that code would get executed.

You can do it nested and in the order that the question asks if it will be easier to understand. We just update the discount as necessary. Note that the other answers might be prettier, but this is a way to do it nested:

double cost = ...;
double discount = 0.0;

if (cost > 2000)
{
   discount = 0.05;
   if (cost > 5000) 
   { 
      discount = 0.10;
      if (cost > 10000)
      {
         discount = 0.20;
      } 
   }  
}

cost = cost - (cost * discount);

You could do it backwards, or with else if and else statements too.

if (cost > 10000)
    discount = 0.2;
else if (cost > 5000)
    discount = 0.1;
else if (cost > 2000)
    discount = 0.05;

Hope the below code can help you......

if (cost > 10000)
    discount = 0.2;
else if (cost > 5000)
    discount = 0.1;
else if (cost > 2000)
    discount = 0.05;
else
System.out.println("cost is less than 2000 hence no discount applied");

I don't think nested if statements are necessary in this case. Instead you can reverse the order of the if statements so that it first checks for cost > 10000 , then cost > 5000 , and finally cost > 2000 .

Also, if the net amount you mentioned is the cost after discount, then you can do a simple calculation after the if statements. It would look something like this:

double discount = 0;

if (cost > 10000){
    discount = 0.2;
}
else if(cost > 5000){
    //similar to above, won't spoon-feed this
}
else if(cost > 2000){
    //similar to above, won't spoon-feed this
}

double netAmount = cost * (1 - discount);

I know you asked for nested if statements, but you could seriously shorten the solution to this problem using a ternary operator, especially since this is primarily used for assigning a value to discount . Probably considerably more complex than what you were looking for but this could work:

double cost, netAmount;
//get cost 
double discount = cost > 10000 ? 0.2 : cost > 5000 ? 0.1 : cost > 2000 ? 0.05 : 0;
netAmount = discount * cost;

Brief Explanation

This works just like nested if statements, but it's a neater, if less readable, way of writing them. Each boolean expression, such as

cost > 10000

is evaluated to either or true or false, as per all boolean expressions. The values after the ternary operator, the ? , are assigned if the expression returns true. Values after the : are assigned if the expression returns false. In this case if cost is greater than 10000, the value 0.2 is assigned. If cost is less than 10000, we want to evaluate another boolean expression so we nest another boolean expression like so:

cost > 10000 ? 0.2 : cost < 5000...

The final : indicates what value will be assigned should all other expression evaluate to false, just like a terminating else statement in an if - else block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM