Question: On java - Prompt the user for four values (i.e. quarters, dimes, nickels and pennies). Calculate and display the total amount as dollars and cents.

On java - Prompt the user for four values (i.e. quarters, dimes, nickels and pennies). Calculate and display the total amount as dollars and cents. User input is shown in italics.

Below is the code I have: However, when I input Quarters 10 Dimes 10 Nickles 10 Pennies 10 its out put is $4.10 and is expected/supposed to be 4.1 how do I fix that ?

import java.util.*; public class Dollars { public static void main(String[] args) { int Quarters; int Dimes; int Nickels; int Pennies; double Dollars; System.out.print("Quarters: "+"Dimes: "+"Nickels: "+"Pennies: "); Scanner scnr = new Scanner(System.in); Quarters = scnr.nextInt(); Dimes = scnr.nextInt(); Nickels = scnr.nextInt(); Pennies = scnr.nextInt(); Dollars = (0.25 * Quarters) + (0.10 * Dimes) + (0.05 * Nickels) + (0.01 * Pennies); String dollarsAsString = String.format ("%1.2f", Dollars); System.out.println("Dollars: "+"$"+ (dollarsAsString)); }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!