Cookie Calories A bag of cookies holds 40 cookies. The calorie information on the bag claims that there are 10 servings in the bag and that a serving equals 300 calories. Write a program that lets the user enter the number of cookies he or she actually ate and then reports the number of total calories consumed.

Respuesta :

A program that lets the user enter the number of cookies he or she actually ate and then reports the number of total calories consumed:

Input Program:

import  javom.sming.30optionPane;  

public class CookieCalories{  

public  static void main ( String [ ] orgs ) {

String userStringoutput;

double userNumberOfCookies;

double userNumberOfServings;

double userNumberOfCalories;

// declare variables

userStringOutput - 3OptionPane.showInputDialog( “Please enter the number of cookies” );

userNumberOfCookies = Double.porseDouble( userStringOutput );

userNumberOfServings = ( userNumberOfCookies / 40 ) * 10;

userNumberOfCalories = ( userNumberOfServings / 1 ) * 300;

3OptionPane.show MessageDialog( null, userNumberOfCookies + “ cookies is equal  to “ + userNumberOfCalories +  “ calories ( “ + userNumberOfServings + “ servings )” );

System.exit( 0 );

}

}

Output Result:

1.  Please Enter No of Cookies: 40

   Result: 40 cookies is equal to 3000 calories (per 10 servings)

2.  Please Enter No of Cookies: 4

    Result: 4 cookies is equal to 300 calories (per 10 servings)

3.  Please Enter No of Cookies: 8

    Result: 8 cookies is equal to 600 calories (per 10 servings)