java 硬币_如何获得Java中的硬币数量?

我有我的代码,用户输入一定数量的钱,输出显示二十,十,五,一,四分之一,硬币,镍币和便士的数量, but 我希望用户输入一定数量的硬币(例如36并得到 ONLY COINS 的数字,这使得36美分 . 这意味着我应该得到1分,1分和1分 . 请有人帮我这个 . 非常感谢!

NOTE: DecimalFormat class is not neccessary

这是我的代码:

import java.util.Scanner;

import java.text.DecimalFormat;

public class Compu

{

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

DecimalFormat decimalFormat = new DecimalFormat("0.00");

System.out.println("Please Enter an amount of Money:");

double change = input.nextDouble();

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("Input entered by user: " + "$" + 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");

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值