2
public class Change {
public static void main(String[] args) {
System.out.print("Enter the amount due: ");//Variable = amount
Scanner keyboard = new Scanner(System.in);
int amount = keyboard.nextInt();
System.out.print("Enter the amount recieved from Customer: ");
Scanner keyboard2 = new Scanner(System.in);
int customer = keyboard2.nextInt();//variable = customer
double change = customer - amount;
System.out.println("You need to give to customer");
double remainingAmount = (double) (change * 100);
double numDollars = remainingAmount/100;
remainingAmount = remainingAmount % 100;
double numQuarters = remainingAmount/25;
remainingAmount = remainingAmount % 25;
double numDimes = remainingAmount/10;
remainingAmount = remainingAmount % 10;
double numNickels = remainingAmount/5;
remainingAmount = remainingAmount % 5;
double numPennies = remainingAmount;
System.out.println("Dollars: " + numDollars);
System.out.println("Quarters: " + numQuarters);
System.out.println("Dimes: " + numDimes);
System.out.println("Nickels: " + numNickels);
System.out.println("Pennies: " + numPennies);
}
}
当我尝试运行代码,我只收到回其余 例如,当我需要为247
金额类型并输入客户编号315
我得到68
为余额在“美元”的第一个陈述中:如何让我的代码打印出更改?我用模但它的唯一打印出剩余
*我使用模数,但它只是打印出余数*呃,这就是模数... – Li357
是的,我知道,但我想我没有正确地说出来,我试图取模数,然后将其分解再往下走进下一个宿舍。例如,在我的例子中,所需的金额为247。那么收到的金额是315.其余的68是正确的?我怎样才能让它跳过这一点,并从分配到100美元的美元开始到宿舍?并且宿舍是序列中的下一个;但它只打印出第一个变量的其余部分。对不起! –
你的代码中的逻辑实际上对我来说看起来很好。您使用分区确定给定硬币的数量,然后使用模数将余数转换为尝试使用较小的硬币。你的输出是什么意外? –