Okay, I'm a total noob at java and am writing a simple program for a class. The program works,
but not as I want it to. I really need it to display the total decimals for the totalProfit and totalCost outputs. Also, if it's
not too much trouble I'd like to know what I'm doing wrong.
***********HERE IS THE CODE***********
here are my outputs. (Total Profits should be somewhere around $7.0200000000000005)// Created By: Russell on 02/09/2019 // Program that acquires input of total amount of milk, then calculates and // displays the amounts of cartons produced, cost to create the milk and // total profits gained package exercise14; import java.util.*; public class exercise14 { static Scanner console = new Scanner(System.in); //declare constants static final double CARTON = 3.78; static final double COST = 0.38; static final double PROFIT = 0.27; public static void main(String[] args) { //declare variables int liter; int cartonsNeeded; double totalCost; double totalProfit; //obtain information System.out.println("Enter liters of milk processed: "); liter = console.nextInt(); //calculate cartonsNeeded = (int) (liter/CARTON); totalCost = (int) (liter*COST); totalProfit = (int) (cartonsNeeded*PROFIT); //display results System.out.println("You entered " +liter +" liters."); System.out.println("You require " +cartonsNeeded +" cartons."); System.out.println("Total Cost: $" +totalCost); System.out.println("Total Profits: $" +totalProfit); } }
run:
Enter liters of milk processed:
100
You entered 100 liters.
You require 26 cartons.
Total Cost: $38.0
Total Profits: $7.0
BUILD SUCCESSFUL (total time: 2 seconds)