关于Java的嵌套if语句的问题

关于Java的嵌套if语句的问题,java,if-statement,Java,If Statement,所以我老师在白板上写的问题是:输入产品名称、数量、价格。计算总数。如果总金额大于2000折扣为5%,如果总金额大于5000折扣为10%,如果大于10000折扣为20% 我知道你必须做这种格式 if (cost > 2000) discount = 0.05; 我会把正确的东西放在你喜欢的东西之前 String prodName; int qty, price; double cost; double discount; 我知道我把它放错了,但我认为在执行嵌套if语句之前,它的格式是这

所以我老师在白板上写的问题是:输入产品名称、数量、价格。计算总数。如果总金额大于2000折扣为5%,如果总金额大于5000折扣为10%,如果大于10000折扣为20%

我知道你必须做这种格式

if (cost > 2000)
 discount = 0.05;
我会把正确的东西放在你喜欢的东西之前

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

我知道我把它放错了,但我认为在执行嵌套if语句之前,它的格式是这样的。我真的需要帮助。我不需要得到这个问题的答案,因为这是最大程度的填鸭式的。我只需要一些指针和一个指南就可以知道该做什么。

只要几个指针,你就可以比较价格,如果你按相反的顺序做,你可以做:

if (cost > 10000)
    discount = 0.2;
else if (cost > 5000)
    discount = 0.1;
else if (cost > 2000)
    discount = 0.05;
这很方便,因为您没有嵌套if语句,而且它仍然是正确的

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);

使用if和else if时,如果第一个if语句为true,则执行该代码,而不检查任何其他if。如果第一个If为false,则按照If后面的顺序检查每个If。如果其中一个为真,则执行该代码,而不检查其他代码。然而,如果没有一个是真的,并且您有一个else块,那么代码将被执行

首先像以前一样声明所有变量:

String prodName="";
int qty, price;
double cost; //your cost will be stored here
double discount =0;
然后做你的计算

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

您可以嵌套并按照问题询问的顺序进行操作,以便于理解。我们只是根据需要更新折扣。请注意,其他答案可能更漂亮,但这是一种方法:

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);

你可以倒着做,或者用
else如果
else
语句也可以。

希望下面的代码能帮助你

if (cost > 10000)
    discount = 0.2;
else if (cost > 5000)
    discount = 0.1;
else if (cost > 2000)
    discount = 0.05;
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");

我不认为嵌套if语句在这种情况下是必要的。相反,您可以颠倒if语句的顺序,以便它首先检查
cost>10000
,然后检查
cost>5000
,最后检查
cost>2000

此外,如果您提到的净金额是折扣后的成本,那么您可以在if声明后进行简单计算。它看起来像这样:

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);

我知道您要求使用嵌套的
if
语句,但您可以使用
三元
运算符大大缩短此问题的解决方案,特别是因为它主要用于为
折扣
赋值。可能比您所寻找的要复杂得多,但这可能有效:

简要说明

这就像嵌套的
if
语句一样,但它是一种更整洁、可读性更低的编写方式。每个布尔表达式,例如

cost > 10000
根据所有布尔表达式计算为true或false。如果表达式返回true,则在
三元
运算符之后的值即为
。如果表达式返回false,则在
之后赋值。在这种情况下,如果成本大于10000,则指定值0.2。如果成本小于10000,我们要计算另一个布尔表达式,因此我们嵌套另一个布尔表达式,如下所示:

cost > 10000 ? 0.2 : cost < 5000...
成本>10000?0.2:成本<5000。。。

最后一个
指示如果所有其他表达式的计算结果为false,将分配什么值,就像
if-else
块中的终止
else
语句一样。

首先,你需要彻底阅读你的书。明显地首先学习
if-then
的工作原理,然后学习
if-then-else
,最后学习
if-then-else-if-then-else
。现在让我们分析您的问题:

输入产品名称、数量、价格。计算总数。如果总金额大于2000折扣为5%,如果总金额大于5000折扣为10%,如果大于10000折扣为20%

很明显,您将设置一个变量,我们称之为折扣,并检查一些条件。为了便于解释,假设总金额大于10000,那么您将设置
折扣=20%
。但要小心,当总量大于10000时,也会导致总量大于5000或2000!那么您将分别设置
折扣=10%
还是
折扣=5%
?不,你不是

因此,为了解决您的问题,如果已经匹配了更高优先级的条件,则不需要检查任何其他条件。因此,我们将采取以下措施:

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
}
现在这里发生了什么?如果第一个测试表达式为
true
total\u amount>10000
),则它将在其正下方的括号内执行代码,而不会执行其他块。但是,如果第一个测试表达式为
false
,它将检查第二个测试表达式(
total\u amount>5000
)。如果第二个测试表达式为
true
,则它将在其正下方的大括号
{}
中执行语句,而不会执行其他块。这一进程仍在继续。如果所有测试表达式均为
false
,则执行
else
中的代码,程序控制跳到
If else
下面


当您完全理解了
if else
之后,您可以用许多不同的方法解决这个问题。祝你好运。

p.S.我还需要得到净金额。看看这个:你需要用嵌套的ifs来做这个,还是这只是你的想法?@Lister这是我的想法,因为除了教授教我的格式之外,我没有其他格式,这些格式是基本的东西,比如找出预科、期中考试、,最终评分或找到某物的总量并放入产品name@IzzyDeLeon你说的净金额是指折扣后的成本吗?我想知道我是否记下了正确的变量t
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
}