Question
Java Branches code not working please HELP!! I am trying to build this class that will ask for an ingredient, and the number of cups
Java Branches code not working please HELP!!
I am trying to build this class that will ask for an ingredient, and the number of cups of the ingredient. It should then branch into an if/else branch off to verify that the number entered is valid and that it is within range. Once that branch completes it should then continue on to ask for the calories per cup, and then calculate total calories. Once I get it to enter the branch though I get a compile error and the program terminates. I am not sure how to nest the branch in with the rest of the code I guess. Thank you in advance to anyone that can help me out with this!
The code is below:
package ingedientclass;
import java.util.Scanner;
/** * * @author */
public class IngedientClass {
public static void main(String[] args) { String nameOfIngredient = ""; double numCups = 0; int numCaloriesPerCup = 0; double totalCalories = 0.0; final int MAX_CUPS = 100; Scanner scnr = new Scanner(System.in); System.out.println("Please enter the name of the ingredient: "); nameOfIngredient = scnr.next(); System.out.println("Please enter the number of cups of " + nameOfIngredient + " we'll need: "); numCups = scnr.nextFloat(); if (numCups > 1 && numCups <= MAX_CUPS){ System.out.println(numCups + " is a valid number of cups"); } else{ System.out.println(numCups + " is not a valid number of cups!"); System.out.println("Please enter a number of cups between 1 and 100:"); numCups = scnr.nextInt(); if (numCups >= 1 && numCups <= MAX_CUPS){ System.out.println(numCups + " is a valid number of cups!"); } else if(numCups <1){ System.out.println(numCups + " is less than 1. Sorry you are out of tries."); } else if (numCups > 100) { System.out.println(numCups + " is larger than 100. Sorry you are out of tries."); } } }else{ System.out.println("Error: That is not a number."); } System.out.println("Please enter the number of calories per cup: "); numCaloriesPerCup = scnr.nextInt(); totalCalories = (numCaloriesPerCup * numCups); System.out.println("The total number of calories is " + totalCalories); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started