有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

我知道你必须做这种格式

if (cost > 2000)
 discount = 0.05;

我会把正确的东西放进去

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

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


共 (6) 个答案

  1. # 1 楼答案

    首先,你需要彻底阅读你的书。显然this can be a good start。首先学习if-then是如何工作的,接下来是if-then-else,最后是if-then-else if-then-else。现在让我们分析一下你的问题:

    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%.

    显然,您将设置一个变量,让我们将其称为discount,并检查一些条件。为了解释,假设总金额大于10000,那么您将设置^{。但要小心,当总量大于10000时,也会导致总量大于5000或2000!那你要分别设置discount = 10%还是discount = 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
    }
    

    现在这里发生了什么?如果第一个测试表达式是truetotal_amount > 10000),它将在它正下方的大括号{ }内执行代码,而不会执行其他块。但是如果第一个测试表达式是false,它会检查第二个测试表达式(total_amount > 5000)。如果第二个测试表达式是true,它会在它正下方的大括号{ }内执行语句,而不会执行其他块。这一过程仍在继续。如果所有测试表达式都是false,则执行else内的代码,程序的控制跳转到if-else之下

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

  2. # 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 ifelse语句

  3. # 3 楼答案

    希望下面的代码能帮助你

    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");
    
  4. # 4 楼答案

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

    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;
    }
    
  5. # 5 楼答案

    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块,那么代码将被执行

  6. # 6 楼答案

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