lundi 8 octobre 2018

Variable not updating in nested if/else statement

I have the following code:

System.out.println("How many sundaes would you like to build?");
int numSundaes = in.nextInt();

int i;

for (i = 0; i <= numSundaes; ++i) {
    System.out.println("On sundae " + i + " how many scoops of ice cream? Each scoop costs $2.50.");
    int numScoops = in.nextInt();

    System.out.println("On sundae " + i + " how many toppings? Each topping costs $0.85.");
    int numToppings = in.nextInt();

    System.out.println("On sundae " + i + " would you like hot fudge? Answer \"yes\" or \"no\". Hot fudge costs $1.50 by itself, or $2.50 for both hot fudge and bananas.");
    String fudgeChoice = in.nextLine();

    System.out.println(fudgeChoice);

    //Skip next line
    in.nextLine();

    System.out.println("On sundae " + i + " would you like bananas? Answer \"yes\" or \"no\". Bananas cost $2.00 by themselves, or $2.50 for both hot fudge and bananas.");
    String bananaChoice = in.nextLine();

    double scoopCost = 2.50;
    double toppingCost = 0.85;
    double fudgeCost = 1.50;
    double bananaCost = 2.00;
    double bananafudgeCost = 2.50;
    double sundaeCost = 0.0;

    if (fudgeChoice.equals("yes") && bananaChoice.equals("yes")) {
        sundaeCost = (scoopCost * numScoops) + (toppingCost * numToppings) + bananafudgeCost;

    } else if (fudgeChoice.equals("yes") && bananaChoice.equals("no")){
        sundaeCost = (scoopCost * numScoops) + (toppingCost * numToppings) + fudgeCost;

    } else if (fudgeChoice.equals("no") && bananaChoice.equals("yes")) {
        sundaeCost = (scoopCost * numScoops) + (toppingCost * numToppings) + bananaCost;
    }

    System.out.println("Cost of sundae " + i + " is " + sundaeCost);


}

}
}

When I run the code, variable sundaeCost does not update – it just stays equal to 0.00, which is what I initialized it as. I want sundaeCost to update in the nested if/else statement, why isn't it working?

Aucun commentaire:

Enregistrer un commentaire