收银机程序



嗨,我在编写这个收银机程序时遇到问题。特别是输出2方法。我试图找到一种方法来输出用户将收到的特定美元金额,而无需将其写为:

system.out.println("您的更改将在百元钞票上显示为"+百+"+五十+"五十美元钞票 ETC"(;

但是,如果美元值

符合用户将收到的更改,请改为包括美元值。

我希望这不会太令人困惑而无法阅读。

int ranNum, hundred, fifty, twenty, ten, five, one, quarter, dime, nickel, penny;
double original, discount, savings, salesprice, tax, total, payment, change, quarterV = .25, dimeV = .10, //V as in value
        nickelV = .05, pennyV = .01;
DecimalFormat percent = new DecimalFormat("0%");
DecimalFormat money = new DecimalFormat("$0.00");
public void input()
{
    System.out.println("Hello, this program will ask you for a price of an item you would like to purchase");
    System.out.println("and return a random discount from 5-75 (multiple of 5). Then return the following:");
    System.out.println("original price, discount percent, discount amount, sales price, tax and total price with 7% tax.n");
    System.out.println("Please give me the price of an item you would like to purchase");
    original = scan.nextDouble();
    scan.nextLine();
}
public void calculations()
{
    //This will be used to find the random discount given to the user:
    ranNum = random.nextInt(15)+1;
    discount = ((ranNum*5)*.10)*.10;
    //This will be used to find the amount the user will save:
    savings = (discount*original);
    //This will be used to find the salesprice of the item being purchased:
    salesprice = original - savings;
    //This will be used to find the total price of the item after 7% tax deductions
    tax = (salesprice*7)/100;
    //This will be used to find the final total the customer must pay
    total = salesprice + tax;
}
public void change()
{
    change = payment - total;
    hundred = (int) Math.floor(change/100);
    fifty = (int) Math.floor((change - hundred * 100)/50);
    twenty = (int) Math.floor((change - hundred * 100 - fifty * 50) / 20);
    ten = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20) / 10);
    five = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10) / 5);
    one = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5) / 1);
    quarter = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1)
            / quarterV);
    dime = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1 
            - quarter * quarterV) / dimeV);
    nickel = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1 
            - quarter * quarterV - dime * dimeV) / nickelV);
    penny = (int) Math.floor((change - hundred * 100 - fifty * 50 - twenty * 20 - ten * 10 - five * 5 - one * 1 
            - quarter * quarterV - dime * dimeV - nickel * nickelV) / penny);
}

public void output1()
{
    System.out.println("The original price of your item was "+money.format(original));
    System.out.println("You will be granted a " +percent.format(discount)+ " discount on your purchase.");
    System.out.println("Your discount amount (amount you are saving) is "+money.format(savings)+".");
    System.out.println("The sales price of your item is "+money.format(salesprice));
    System.out.println("The 7% tax payment will come out to be "+money.format(tax));
    System.out.println("Thus your total will be "+money.format(total)+"n");
    System.out.println("How much money are you using to purchase your item?");
    payment = scan.nextDouble();
    scan.nextLine();
}
public void output2()
{
    System.out.println("Your change is");
    if (change>=100)
    {
        System.out.println(hundred+" one hundred dollar bills");
    }
    else
    {
        if (change>=50)
        {
            System.out.println(fifty+" fifty dollar bills");
        }
        else
        {
            if (change>=20)
            {
                System.out.println(twenty+" twenty dollar bills");
            }
            else
            {
                if (change>=5)
                {
                    System.out.println(five+" five dollar bills");
                }
                else
                {
                    System.out.println(one+" one dollar bills");
                }
            }
        }
    }

}

public void run()
{
    input();
    calculations();
    output1();
    change();
    output2();
}
public static void main(String[] args)
{
    CashRegister phill = new CashRegister();
    phill.run();
}

}

试试这个。我只做了100、50和20美元钞票的收银机,因为你可以从这里掌握它的窍门。此代码所做的只是从最高面额的钞票到最低面额的钞票,从零钱金额中减去钞票数量以获得剩余的零钱量。

public void output2()
{
    int remainder = change; //make a copy of change variable so it remains final.
    int count=0;
    System.out.println("Your change is");
    if(remainder>=100){
        hundred = (int)remainder/100;
        remainder %= 100;
    }
    if(remainder>=50){
        fifty = (int)remainder/50;
        remainder %= 50;
    }
    if(remainder>=20){
        twenty = (int)remainder/20;
        remainder %= 20;
    }
    ...
    System.out.print(hundred + " one hundred dollar bills,");
    System.out.print(fifty + " fifty dollar bills,");
    System.out.print(twenty + " twenty dollar bills,");
    ...
}

这可以解决问题

public void output2(){
    String changeMessage = "";
    double _change = change; //In case you need the change value somewhere else
    int numberOfNotesInHundred = (int)(_change / 100);
    if (numberOfNotesInHundred > 0){
        changeMessage += (numberOfNotesInHundred + " one hundred dollar bills ");
        _change = _change % 100;
    }
    int numberOfNotesInFifty = (int)(_change / 50);
    if (numberOfNotesInFifty > 0){
        changeMessage += (numberOfNotesInFifty + " fifty dollar bills ");
        _change = _change % 50;
    }
    // And so on for 20, 5 and 1
    System.out.println("Your change will be: " + changeMessage);
}

最新更新