Question
I have this code and it outputs like this: Enter total amount: $ Quarter: 0 Dime: 0 Nickel: 0 Pennies: 0 but I need it
I have this code and it outputs like this: Enter total amount: $ Quarter: 0 Dime: 0 Nickel: 0 Pennies: 0
but I need it to output like this:
Enter total amount: $You have 0 quarters, 0 dimes, 0 nickels, and 0 pennies.
package edu.wit.cs.comp1050;
/** * Solution to the first programming assignment * When it run it outputs "Hello World!" to the terminal * * @author Rose Levine * */ import java.util.Scanner; public class PA1c { /** * Error message to display for negative amount */ public static final String ERR_MSG = "Dollar amount must be non-negative!"; public static int[] compute_coins(int coin_value ,int num, int amount_left){ int[] arr=new int[2]; if(!(coin_value> 0 && coin_value<100)){ System.out.println("Coin value is invalid: "); return arr; } else{ num = amount_left/coin_value; amount_left = amount_left%coin_value; arr[0]=num; arr[1]=amount_left; } return arr; } /** * Method to convert a double to * an integer * * @param num number to convert * @return converted value */ public static int convertToInt(double num) { return (int) Math.round(num); }
// TODO: document this method public static void main(String[] args) { int amount_left; int num; char c; double price; double money; num = 0; Scanner sc=new Scanner(System.in); System.out.print("Enter total amount: $"); money=sc.nextDouble(); if(money<0) System.out.println(ERR_MSG); else { money=money*100.0; double diff = money; amount_left = convertToInt(diff); int[] arr=new int[2]; arr=compute_coins(25,num,amount_left); num=arr[0]; amount_left=arr[1]; System.out.println(" Quarter: "+num);
if((amount_left>= 0)){ arr=compute_coins(10,num,amount_left); num=arr[0]; amount_left=arr[1]; System.out.println(" Dime: "+num);
}else{ System.out.println(" Dime: 0");
} if((amount_left>= 0)){ arr=compute_coins(5,num,amount_left); num=arr[0]; amount_left=arr[1]; System.out.println(" Nickel: "+num);
}else{ System.out.println(" Nickel: 0");
} if((amount_left>= 0)){ arr=compute_coins(1,num,amount_left); num=arr[0]; amount_left=arr[1]; System.out.println(" Pennies: "+num);
}else{ System.out.println(" Pennies: 0"); }
} } }
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