【问题标题】:My "Else If" Code Isn't Working As Expected我的“Else If”代码没有按预期工作
【发布时间】:2022-10-06 11:42:03
【问题描述】:

我是一个新的编码员。正在执行一项任务。这也是我在这里的第一篇文章,如果有点草率,我深表歉意。

我在使用 Java 中的 if/else 语句时遇到了一些问题……“if”条件似乎可以正常工作。但我的“其他”条件没有。看看下面的代码和构建结果。

基本上,我输入一种成分。然后我放入所需的杯子数量。以及每 x 杯该成分所含的卡路里数。只要我输入我想要的“成功”结果,这一切似乎都有效。

Successful Build Image

但是当我开始输入我的标准之外的值时,我的应用程序似乎并不关心。如果我输入 0,我应该得到“你的响应无效”的输出或我编码的任何内容。但它似乎完全跳过了这一点。

Bad Code Image

package recipe_collection_manager;

import java.util.Scanner;

public class Ingredient {


    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        //Initializes the variables

        String nameOfIngredient = \"\";
        int numberCups = 0;
        int numberCaloriesPerCup = 0;
        int totalCaloriesPerCup = 0;
        double totalCalories = 0.0;

// Enter the name of the ingredient.

        System.out.println(\"Please enter the name of the ingredient: \");
        nameOfIngredient = scnr.next();

        // Enter the number of cups needed for the ingredient.
        // If Else statements used to establish if the number of cups is valid.

        System.out.println(\"Please enter the number of cups of \"
                + nameOfIngredient + \" we\'ll need. The number of cups must be between 1 and 100: \");
        numberCups = scnr.nextInt();

        if (numberCups >= 1 || numberCups <= 100) {
            System.out.println(\"The number of cups is valid.\");
        } else if (numberCups <= 1 || numberCups >= 100) {
            System.out.println(\"The number you have entered is invalid.  Please try again.\");
        }

        // Enter the number of calories used per cup.
        // If Else statements are used to establish if the number of calories is valid.

        System.out.println(\"Please enter the number of calories per cup: \");
        numberCaloriesPerCup = scnr.nextInt();

        if (numberCaloriesPerCup >= 1 || numberCaloriesPerCup <= 1000) {
            System.out.println(\"The number of calories is valid.\");
        } else if (numberCaloriesPerCup <= 1 || numberCaloriesPerCup >= 1000) {
            System.out.println(\"The number you have entered is invalid.  Please try again.\");
        }

        // Calculation for totalCalories based on numberCups and numberCaloriesPerCup

        if (numberCups > 0 && numberCaloriesPerCup > 0) {
            totalCalories = numberCups * numberCaloriesPerCup;
        }

        System.out.println(nameOfIngredient + \" uses \" + numberCups
                + \" cups and has \" + totalCalories + \" calories.\");

    }
}
  • numberCups &gt;= 1 || numberCups &lt;= 100 读取“如果杯子的数量至少为 1,或者如果杯子的数量达到 100(或两者),则执行...\”。这是你想要的逻辑吗?
  • 以@nanofarad 所说的内容为基础。 0 小于或等于 100。因此您的 if 语句对于 0 的值是正确的。
  • 我明白你在说什么。所以这个数字必须在 1 到 100 之间。如果这样更有意义的话。 0 应该失败,101+ 应该失败。
  • 正确的。因此,请检查该逻辑以查看您是否在代码中正确表达了您的要求。
  • 该值有效或无效;你想要一个&amp;&amp;(不是||)和一个else(不是else if)。

标签: java


【解决方案1】:

问题是:

 if (numberCups >= 1 || numberCups <= 100) {
 ...
 }

当您输入 0 时,程序会检查 0 是否大于或等于 1,这是错误的,但您也有“或”条件( || ),在这种情况下,您正在检查 0 <= 100 是否为真, 假 || true 给出 true ,这就是为什么你的 if 语句是正确的。您需要使用“and”( && )而不是“or”。你的逻辑有缺陷。

下面的测试代码,它现在应该可以工作了:

package recipe_collection_manager;
    
    import java.util.Scanner;
    
    public class Ingredient {
    
    
        public static void main(String[] args) {
            Scanner scnr = new Scanner(System.in);
    
            //Initializes the variables
    
            String nameOfIngredient = "";
            int numberCups = 0;
            int numberCaloriesPerCup = 0;
            int totalCaloriesPerCup = 0;
            double totalCalories = 0.0;
    
    // Enter the name of the ingredient.
    
            System.out.println("Please enter the name of the ingredient: ");
            nameOfIngredient = scnr.next();
    
            // Enter the number of cups needed for the ingredient.
            // If Else statements used to establish if the number of cups is valid.
    
            System.out.println("Please enter the number of cups of "
                    + nameOfIngredient + " we'll need. The number of cups must be between 1 and 100: ");
            numberCups = scnr.nextInt();
    
            if (numberCups >= 1 && numberCups <= 100) {
                System.out.println("The number of cups is valid.");
            } else if (numberCups <= 1 || numberCups >= 100) {
                System.out.println("The number you have entered is invalid.  Please try again.");
            }
    
            // Enter the number of calories used per cup.
            // If Else statements are used to establish if the number of calories is valid.
    
            System.out.println("Please enter the number of calories per cup: ");
            numberCaloriesPerCup = scnr.nextInt();
    
            if (numberCaloriesPerCup >= 1 || numberCaloriesPerCup <= 1000) {
                System.out.println("The number of calories is valid.");
            } else if (numberCaloriesPerCup <= 1 || numberCaloriesPerCup >= 1000) {
                System.out.println("The number you have entered is invalid.  Please try again.");
            }
    
            // Calculation for totalCalories based on numberCups and numberCaloriesPerCup
    
            if (numberCups > 0 && numberCaloriesPerCup > 0) {
                totalCalories = numberCups * numberCaloriesPerCup;
            }
    
            System.out.println(nameOfIngredient + " uses " + numberCups
                    + " cups and has " + totalCalories + " calories.");
    
        }
    }

【讨论】:

  • 输入有效或无效。我认为 OP 不需要else if。这是一个直else。我还建议卡路里可以为零。水的卡路里为零。但这更像是一个需求问题而不是编程问题。
【解决方案2】:

只是为了澄清。以下声明

if(numberCups >= 1 || numberCups <= 100) {
...
}

任何数量的杯子都是真的。第一个条件将捕获任意数量的杯子&gt;= 1。任意数量的杯子&lt;= 0 都将被第二个条件捕获,因为在这种情况下它们都将小于100。对于逻辑 ||,只需要一个条件为真,该语句为真。

实际上,

if(numberOfCups is >= A || numberOfCups <= B) {
...
}

只要B &gt;= A-1 就永远是真的。

【讨论】:

    【解决方案3】:

    我不认为那是你的问题。它在这里工作得很好。my console

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 2020-07-26
      • 2022-11-20
      • 2017-06-18
      • 1970-01-01
      • 2020-03-11
      • 2013-12-16
      相关资源
      最近更新 更多