简体   繁体   English

如何获取Java中的硬币数量?

[英]How to get the number of coins in Java?

I have my code where user enters an amount of money and the output displays the number of Twenties, tens, fives, ones, quarters, dimes, nickels and pennies, but I would like the user to enter an amount of coins (for example 36) and get the number of ONLY COINS that makes for the 36 cents. 我有我的代码,用户输入金额,输出显示二十,十,五,一,四分之一,角钱,镍和几美分的数量, 我希望用户输入一定数量的硬币(例如36 ),然后获取可赚取 36美分的唯一硬币数量。 Meaning that I should get 1 Quarter, 1 dime, and 1 pennie. 这意味着我应该得到1个季度,1个角钱和1个便士。 Please someone help me on this. 请有人帮我。 Thanks a lot! 非常感谢!

NOTE: 注意:
DecimalFormat class is not neccessary DecimalFormat类不是必需的

Here's my code: 这是我的代码:

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");

  }
}

The modulus operator is your friend: 模运算符是您的朋友:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter amount in cents");
    double cents = input.nextInt();

    int numQuarters = cents / 25;
    int numDimes    = (cents % 25) / 10;
    int numNickels  = ((cents % 25) % 10) / 5;
    int numPennies  = ((cents % 25) % 10) % 5;

    System.out.println(quarters + " Quarters");
    System.out.println(dimes    + " Dimes");
    System.out.println(nickels  + " Nickels");
    System.out.println(pennies  + " Pennies");
}

To do this, completely remove the part of the program that deals with dollars, and feed the user input directly to 'cents'. 为此,请完全删除程序中与美元有关的部分,并将用户输入直接输入“分”。

Also, this part: 另外,这部分:

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;

Will be inaccurate (in a few cases. Not all), as you aren't changing the value of 'cents'. 由于您没有更改“分”的值,因此可能会不准确(在某些情况下,不是全部)。 Therefore, if you input '36', it will return 1 quarter (25), 1 dime (35), 1 nickel (40), and 1 penny (41). 因此,如果输入“ 36”,它将返回四分之一(25),1角钱(35),1镍(40)和1美分(41)。

To avoid this, use the following code: 为避免这种情况,请使用以下代码:

int cents = (int)cents5;
int quarters = cents / 25;
int cents1 = cents % 25;
int dimes = cents1 / 10;
int cents2 = cents1 % 10;
int nickels = cents2 / 5;
int cents3 = cents2 % 5;
int pennies = cents3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM