
題目如下:
Write an application that prompts for and reads a double value representing a monetary amount. Then determine the fewest number of each bill and coin needed to represent that amount, starting with the highest (assume that a ten-dollar bill is the maximum size needed). For example, if the value entered is 47.63 (forty-seven dollars and sixty-three cents), then the program should print the equivalent amount as:
$ 47.63
4 ten dollar bills
1 five dollar bills
2 one dollar bills
2 quarters
1 dime
0 nickel
3 pennies
PS:quarters=0.25 ;1 dime =0.05;nickel =0.1; pennies=0.01
import java.util.Scanner;
public class Money
{
public static void main(String [] args)
{
double amount;
int one, five, ten;
int penny, nickel, dime, quarter;
Scanner kb = new Scanner(System.in);
System.out.print("What is the amount? ");
amount = kb.nextDouble();
int a=(int)amount;
ten = a/10;
amount = amount;
five =(int) amount/5;
amount = amount%5;
one = (int)amount/1;
amount = amount%1;
quarter = amount/.25;
amount = amount%.25;
dime = a/.10;
amount = amount%.10;
nickel = a/.05;
amount = amount%.05;
penny = a/.01;
amount = amount%.01;
System.out.println(ten + " tens.\n\n");
System.out.println(five + " fives.\n\n");
System.out.println(one + " ones.\n\n");
System.out.println(quarter + " quarters.\n\n");
System.out.println(dime + " dimes.\n\n");
System.out.println(nickel + " nickels.\n\n");
System.out.println(penny + " pennies.\n\n");
}
}