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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started