编译完美,但我只想知道如何使未使用的硬币不出现。IE。用户输入:9.65
所以输出将是:
4 toonies 1 loony 2 Quarters 1 角钱 1 镍
买我的显示:
您输入的金额为 9 美元和 65 美分。
要弥补这一数额,请使用
4 toonies 1 loony 2 Quarters 6 角钱 13 镍币 15 便士
处理结束
编辑:
import javax.swing.JOptionPane; // Needed for JOptionPane
import java.util.Scanner;
/**
This program is for question 1.
need to use echo and make only needed coins appear and also space between lines
*/
包装测试;
public class coincounterr {
public static void main (String[] args){
//Declare variables
String input; //to ask user an amount of money
double money; //money user types in
int dollars;
double cents;
long t, l, q, d, n, p;
final int QUARTERS = 25;
final int DIME = 10;
final int NICKEL = 5;
final int PENNIES = 1;
//Ask user for an amount of money
input = JOptionPane.showInputDialog(null, "Enter any amount of money in $.");
money = Double.parseDouble(input);
//use println to display the end result
dollars = (int) money;
long cent = Math.round((money-Math.floor(money))*100);
System.out.println("You entered the amount " + dollars + " dollars and " + cent + " cents.\n");
t = dollars/2; //2 dollars
l = dollars%2; //1 dollar
q = cent/25; //25 cents
d = (cent%25)/10; //10 cents
n = (cent - (q*25)-(d*10))/5; //5 cents
p = cent - (q*25)-(d*10)-(n*5); //1cent
// output for toonies and loonies
System.out.println("To make up this amount, use \n");
if ( t<= 0)
System.out.println();
else if ( t > 1)
System.out.println(+t+" toonies \n");
else if ( t <= 1)
System.out.println(+t+" toony \n");
if (l<=0)
System.out.println();
else if ( l > 1)
System.out.println( + l + " loonies \n");
else if ( l <= 1)
System.out.println( +l+ " loony \n");
//use if statement for QUARTERS
if (q <= 0)
System.out.println();
else if (cent >= QUARTERS) //25 cents
System.out.println( +q+ " quarters \n");
if (d <= 0)
System.out.println();
else if ( cent >= DIME )
System.out.println ( +d+ " dime \n");
if (n <= 0)
System.out.println();
else if ( cent >= NICKEL)
System.out.println( +n+ " nickels\n");
if (p <= 0)
System.out.println();
else if ( cent >= PENNIES)
System.out.println( +p+ " pennies\n");
System.out.println("End of processing");
}
}