Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

Why is it important for a mission statement to be reconciliatory?

Answered: 1 week ago

Question

4. Where do they live? (city or town, state, country)

Answered: 1 week ago

Question

=+ how might this lead to faster growth in productivity?

Answered: 1 week ago