-3

编译完美,但我只想知道如何使未使用的硬币不出现。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");
    }
}
4

1 回答 1

0

if (n > 0)在打印“不必要的”硬币之前添加。我不会发布代码,以便您可以继续练习;)

编辑:大提示:

else if (cent <= NICKEL) {
    if (n > 0)
        System.out.println(n+" nickels");
}

EDIT2:您的计算存在问题:假设您有 65 美分(cent = 65):

d = cent/10; // 6
n = cent/5;  // 13, when what you want is probably 1

您忘记减去之前计算的金额d:如果您有 65 美分并决定获得65 / 10 = 610 美分的硬币,则余数为 5 美分。您应该在每个步骤中处理剩余部分。

祝你好运!

于 2013-10-05T06:45:08.383 回答