将模数用于收银程序时遇到麻烦

时间:2019-03-04 23:29:13

标签: java

对于我的收银机模拟,我必须提取应付款并以100、50、20、10、5、1的形式打印出来。我有两个要解决的问题:

1。)是否有一种更简单的方法来计算每张钞票需要多少美元?我的方程式有效,但是很长,我认为有一种更简单的方法可以做到这一点?

2。)在处理美分时,我应该做什么(changeDue%100)? changeDue等于39.12,我尝试拉12美分,但我不知道怎么办?有人可以帮我解释一下吗?


    double changeDue = 39.12;
    double coins;
    double change;
    int hundreds;
    int fifties;
    int twenties;
    int tens;
    int fives;
    int ones;
    int quarters;
    int dimes;
    int nickels;
    int pennies;
    double cents; 

    //calculations for amount of each dollar bill that needs to be given back
    hundreds = (int)(changeDue/100);
    change = changeDue - (hundreds * 100);

    fifties = (int)(change/50);
    change = changeDue - (hundreds * 100) - (fifties * 50);

    twenties = (int)(change/20);
    change = changeDue - (hundreds * 100) - (fifties * 50) - (twenties * 20);

    tens = (int)(change/10);
    change = changeDue - (hundreds * 100) - (fifties * 50) - (twenties * 20) - (tens * 10);

    fives = (int)(change/5);
    change = changeDue - (hundreds * 100) - (fifties * 50) - (twenties * 20) - (tens * 10) - ( fives * 5);

    ones = (int)(change/1);

    //calculations for amount of coins that needs to be given back
    cents = changeDue%100;

    quarters = (int)cents/10;

3 个答案:

答案 0 :(得分:0)

模运算符给出除法后的余数。在您的示例中,changeDue % 100将数字除以100,然后返回余数。如果您提供的数字以美分计,例如3912,则此功能将完全按照您的期望工作。但是,您使用的是美元。因此,首先要乘以100,将美元金额转换为几美分。

注意

ones = (int)(change/1);可以简化为ones = (int) change;

答案 1 :(得分:0)

另一种小于1美元的零钱产生方法是

cents=changeDue-(int)changeDue

答案 2 :(得分:0)

  

1。)是否有一种更简单的方法来计算每张钞票需要多少美元?我的方程式有效,但是很长,我认为有一种更简单的方法可以做到这一点?

我发现了一种更简便的计算方法。请参阅下面的代码和代码注释。

public static void main(String args[]) {
    double changeDue = 39.12; // The change needed
    double change = changeDue; // Used for the calculations of change

    /*
     * Add here possible change, use an array so that you won't have to declare many
     * variables This way if you want to add other possible changes you just have to
     * add it in the array But make sure the possible changes is arrange in
     * descending order
     */
    double[] possibleChange = new double[] { 100, 50, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01 }; // the possible changes
    String[] possibleChangeLabel = new String[] { "Hundreds", "Fifties", "Twenties", "Tens", "Fives", "Ones", "Quarters", "Dimes", "Nickels", "Pennies" }; // used for printing

    int[] coins = new int[possibleChange.length]; // Used for the number of coins needed for each possible change

    /*
     * Loop through all the possible changes and acquire the possible number of
     * coins needed for each change
     */
    for (int i = 0; i < possibleChange.length; i++) {
        coins[i] = (int) (change / possibleChange[i]); // calculate the possible number of coins in the given change
        change = (double) Math.round((change - (coins[i] * possibleChange[i])) * 100) / 100; // Update the change and limit to 2 decimal places
    }

    /*
     * This is for printing
     */
    System.out.println("Your change is $" + changeDue);
    System.out.println("The change is composed of: ");
    for (int i = 0; i < possibleChange.length; i++) {
        if (coins[i] > 0) {
            System.out.println( "\t" + coins[i] + " piece/s of $" + possibleChange[i] + "(" + possibleChangeLabel[i] + ")");
        }
    }
}
  

2。)在处理美分时,我应该做什么(changeDue%100)? changeDue等于39.12,我尝试拉12美分,但我不知道怎么办?有人可以帮我解释一下吗?

我不知道您为什么需要此过程,但是您可以在下面尝试我的代码。

double number = 123.45; // Your change
int decimal = (int) number; // You will get the whole number part (123)
double fractional = number - decimal; // You will get the fraction part (45)