我正在进行一项java任务,您可以在其中输入一个对象的价格,以及理论客户为您提供物品的安装。然后程序会返回你欠他们多少钱,然后将它分解成你应该给他们的美元,四分之一,硬币,镍币和便士。
基本上是运行时的样子
购买价格是多少? (如果有帮助的话,在计算中排除小数 您) $ 98.50 你付多少钱? (不包括小数) $ 100.00 购买价格为98.50美元 你支付了100美元 你收到1.50美元的变化。 0百美元的钞票 0五十美元的钞票 0二十美元的钞票 0十美元的钞票 0五美元的钞票 1美元的钞票 2季 0角钱 0镍(s) 0便士/便士
我理解其中的大部分内容,但我似乎无法理解所交回的变更的细分。到目前为止,我的代码是什么,但如果有人能告诉我如何打破变化。 提前谢谢:)
import java.util。*; 公共类ChangeTendered {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the purchase price: ");
double price = scan.nextDouble();
System.out.println("Enter the amount payed: ");
double ammountPayed = scan.nextDouble();
double changeDue = ammountPayed - price;
int dollars = (int)changeDue;
System.out.println("Return"+ dollars+ "Dollars");
scan.close();
}
}
在附注中,我只是将changeDue转换为int以便砍掉小数并获得应付的美元,如果这引起任何混淆。
答案 0 :(得分:4)
这是一个初步方法
int change = (int)(Math.ceil(changeDue*100));
int dollars = Math.round((int)change/100);
change=change%100;
int quarters = Math.round((int)change/25);
change=change%25;
int dimes = Math.round((int)change/10);
change=change%10;
int nickels = Math.round((int)change/5);
change=change%5;
int pennies = Math.round((int)change/1);
System.out.println("Dollars: " + dollars);
System.out.println("Quarters: " + quarters);
System.out.println("Dimes: " + dimes);
System.out.println("Nickels: " + nickels);
System.out.println("Pennies: " + pennies);
您也可以为货币备注添加更多代码。
答案 1 :(得分:1)
根据我的理解,你需要将退回的钱分成不同的账单:100,50,20,10,5 ......等等。
我认为您可以使用'division'来解决Java中的问题。以下伪代码是您解决问题的方法:
//For example:
double changeDue = 15.5;
double moneyLeft = changeDue;
int oneHundred = moneyLeft / 100;
moneyLeft -= oneHundred * 100;
int fifty = moneyLeft / 50;
moneyLeft -= fifty*50 ;
...
//Just remember to 'floor' the result when divided by a double value:
int quarter = Math.floor( moneyLeft / 0.25);
moneyLeft -= quarter * 0.25 ;
...//Until your minimum requirement.
//Then print out your results.
希望它有所帮助。
答案 2 :(得分:0)
我所做的是将其转换为字符串,然后执行小数拆分以分隔更改和美元。
import java.util.Scanner;
import java.text.DecimalFormat;
public class register
{
public static void main(String[] args)
{
DecimalFormat decimalFormat = new DecimalFormat("0.00");
Scanner kb = new Scanner(System.in);
System.out.print("How much does this item cose? -> ");
double cost = kb.nextDouble();
System.out.print("How many will be purcased? -> ");
double quanity = kb.nextDouble();
double subtotal = (cost * quanity);
double tax = (subtotal * .06);
double total = (subtotal + tax);
System.out.print("How much money has been tendered? -> ");
double tendered = kb.nextDouble();
double change = (tendered - total);
int dollars = (int)change;
int twenties = dollars / 20;
int dollars1 = dollars % 20;
int tens = dollars1 / 10;
int dollars2 = dollars % 10;
int fives = dollars2 / 5;
int dollars3 = dollars % 5;
int ones = dollars3;
String moneyString = decimalFormat.format(change);
String changeString = Double.toString(change);
String[] parts = moneyString.split("\\.");
String part2 = parts[1];
double cents5 = Double.parseDouble(part2);
int cents = (int)cents5;
int quarters = cents / 25;
int cents1 = cents % 25;
int dimes = cents1 / 10;
int cents2 = cents % 10;
int nickels = cents2 / 5;
int cents3 = cents % 5;
int pennies = cents3;
System.out.println("Cost: " + "$" + decimalFormat.format(cost));
System.out.println("Quanity: " + quanity);
System.out.println("Subtotal: " + "$" + decimalFormat.format(subtotal));
System.out.println("Tax: " + "$" + decimalFormat.format(tax));
System.out.println("Total: " + "$" + decimalFormat.format(total));
System.out.println("Tendered: " + "$" + decimalFormat.format(tendered));
System.out.println("Change: " + "$" + moneyString);
System.out.println(twenties + " Twenties");
System.out.println(tens + " Tens");
System.out.println(fives + " Fives");
System.out.println(ones + " Ones");
System.out.println(quarters + " Quarters");
System.out.println(dimes + " Dimes");
System.out.println(nickels + " Nickels");
System.out.println(pennies + " Pennies");
}
}
答案 3 :(得分:0)
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class ChangeTenderedWorking {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the purchase price: ");
BigDecimal price = scan.nextBigDecimal();
System.out.println("Enter the amount paid: ");
BigDecimal amountPayed = scan.nextBigDecimal();
Map<Denomination, Integer> changeDue = getDenomination(amountPayed, price);
for(Denomination denomination : changeDue.keySet()) {
System.out.println("Return " + denomination + " bill(s) : "+ changeDue.get(denomination));
}
scan.close();
}
public static Map<Denomination, Integer> getDenomination(BigDecimal amountPayed, BigDecimal price) {
BigDecimal change = amountPayed.subtract(price);
System.out.println("Return change -- "+ change);
Map<Denomination, Integer> changeReturn = new LinkedHashMap<Denomination, Integer>();
for(Denomination denomination : Denomination.values()) {
BigDecimal[] values = change.divideAndRemainder(denomination.value, MathContext.DECIMAL32);
if(!values[0].equals(BigDecimal.valueOf(0.0)) && !values[1].equals(BigDecimal.valueOf(0.0))) {
changeReturn.put(denomination, values[0].intValue());
change = values [1];
}
}
return changeReturn;
}
enum Denomination {
HUNDRED(BigDecimal.valueOf(100)),
FIFTY(BigDecimal.valueOf(50)),
TWENTY(BigDecimal.valueOf(20)),
TEN(BigDecimal.valueOf(10)),
FIVE(BigDecimal.valueOf(5)),
TWO(BigDecimal.valueOf(2)),
ONE(BigDecimal.valueOf(1)),
QUARTER(BigDecimal.valueOf(0.25)),
DIME(BigDecimal.valueOf(0.10)),
NICKEL(BigDecimal.valueOf(0.5)),
PENNY(BigDecimal.valueOf(0.1));
private BigDecimal value;
Denomination(BigDecimal value) {
this.value = value;
}
}
}