简体   繁体   English

我的“Else If”代码没有按预期工作

[英]My "Else If" Code Isn't Working As Expected

I am a new coder.我是一个新的编码器。 Working on an assignment.在做作业。 This is also my first post here so I apologize if it's a little sloppy.这也是我在这里的第一篇文章,所以如果有点草率,我深表歉意。

I'm having some troubles with my if/else statements in Java...the "if" conditions seem to work okay.我在 Java 中的 if/else 语句中遇到了一些问题...“if”条件似乎工作正常。 But my "else" conditions do not.但是我的“其他”条件没有。 Take a look at the code and the build results below.看看下面的代码和构建结果。

Basically, I enter an ingredient.基本上,我输入一种成分。 And then I put in the number of cups needed.然后我输入所需的杯子数量。 And the number of calories the ingredient has per x cup.以及每 x 杯该成分所含的卡路里数。 That all seems to work as long as I input what I want to for "successful" results.只要我输入我想要的“成功”结果,这一切似乎都有效。

Successful Build Image成功构建图像

But when I start to input values outside of my criteria, my application doesn't seem to care.但是当我开始输入我的标准之外的值时,我的应用程序似乎并不关心。 If I input 0, I should get that output of "your response is invalid" or whatever it is I coded.如果我输入 0,我应该得到“您的响应无效”的 output 或我编码的任何内容。 But it just seems to skip over that entirely.但它似乎完全跳过了这一点。

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

    }
}

Problem was in line:问题是:

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

When you entered 0, program checked if 0 is greater or equal to 1, and that was false but you had also "or" condition ( || ), and in that condition you were checking if 0 <= 100 and because that is true, false ||当您输入 0 时,程序检查 0 是否大于或等于 1,这是错误的,但您也有“或”条件( || ),并且在该条件下您正在检查是否 0 <= 100 并且因为这是真的, 假 || true gives true and that's why your if statement was correct. true 给出 true,这就是为什么你的 if 语句是正确的。 You needed to use "and" ( && ) instead of "or".您需要使用“和”( && ) 而不是“或”。 There was flaw in your logic.你的逻辑有问题。

Test code below, it should work now:下面的测试代码,它现在应该可以工作了:

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.");
    
        }
    }

Just to clarify.只是为了澄清。 The following statement以下声明

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

Is true any number of cups.任何数量的杯子都是如此。 Any number of cups >= 1 will be caught by the first condition.任何数量的杯子>= 1都会被第一个条件捕获。 Any number of cups <= 0 will be caught by the second condition since in that case they will all be less than 100 .任何数量<= 0的杯子都会被第二个条件捕获,因为在这种情况下它们都将小于100 With a logical ||用逻辑|| only one condition is required to be true for the statement to be true.陈述为真只需要一个条件为真。

In fact,实际上,

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

will always be true as long as B >= A-1 .只要B >= A-1就永远为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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