/**
* Finds the amount of change needed, and how to pay it
*
* @author Sean Appaneal
*
* @version v1.0
*
*/
import java.util.*;
import java.util.Scanner;
public class MakeChange
{
public static void main(String[] args) {
//naming variables
int quarters;
int dimes;
int nickles;
int pennies;
//generate random number
int max = 1900;
int min = 100;
int randomNum = (int)(Math.random() * ((max - min) + 1)) + min; //inclusive
double myNum = randomNum/100.00;
//get change
double change = 20.00 - myNum;
//switch chang from a double to an int
int changeInt = (int)(change * 100);
//print out price and change
System.out.println("Price: " + myNum);
System.out.println("Your change is: " + change);
quarters = changeInt / 25;//find # of quarters
changeInt = changeInt % 25;//remove # of quarters
dimes = changeInt / 10;//find # of dimes
changeInt = changeInt % 10;//remove # of dimes
nickles = changeInt / 5;//find # of nickes
changeInt = changeInt % 5;//remove number of nickles
pennies = changeInt;//find pennies
//printing out results
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickles: " + nickles);
System.out.println("Pennies: " + pennies);
}//end of main
}//end of class
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.