Question:
A bag of cookies holds 40 cookies. There are 10 “serving in the bag and that a serving equals
300 calories. Write a program that asks the user to input how many cookies they actually ate
and then reports how many total calories were consumed.
Code:
import java.util.Scanner; public class Cookies { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter how many cookies you ate: "); int cookieCount = sc.nextInt(); int servingSize = 40/10; int caloriesPerCookie = 300/servingSize; int totalCalories = cookieCount * caloriesPerCookie; System.out.println("Total calories consumed: " + totalCalories); } }
Output:
$ java Cookies Enter how many cookies you ate: 6 Total calories consumed: 450