Make Change

/** * change maker * @author Josh Wang * @version 09/13/17 */ import java.util.*; import java.text.DecimalFormat; public class MakeChange{ public static void main(String[] args){ int max = 1900; int min = 100; int rng = (int)(Math.random() * ((max - min) + 1)) + min; //inclusive DecimalFormat format = new DecimalFormat("0.00"); int change = 2000 - rng; int q = change/25; int d = change%25/10; int n = change%25%10/5; int p = change%25%10%5; String qq; String dd; String nn; String pp; if (q == 1){ qq ="1 quarter, "; } else{ qq = q + " quarters, "; } if (d == 1){ dd = "1 dime, "; } else{ dd = d + " dimes, "; } if (n == 1){ nn = "1 nickel, "; } else{ nn = n + " nickels, "; } if (p == 1){ pp = "and 1 penny."; } else{ pp = "and " + p + " pennies."; } System.out.println("\nThe little boat costs $" + format.format(rng/100.0) + "."); System.out.println("You pay with a crisp $20 note.\nYour change is $" + format.format(change/100.0) + "."); System.out.println("The cashier gives you " + qq + dd + nn +pp); } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.