如何添加不同货币的硬币

发布于 2024-09-28 03:41:54 字数 335 浏览 1 评论 0原文

我们正在教授一门需要教授货币知识的课程。我们希望我们的学生使用世界各地的不同货币进行添加和演示。

(e.g. Country=US, How much does 3 nickels + 2 pennies + 3 dimes)
(e.g. Country=UK, ...)
(e.g. Country=JAPAN, ...)
(e.g. Country=CHINA, ....)
(e.g. Country=AUSTRALIA, ...)

有没有示例代码可以演示不同货币的硬币转换?

注意:如果它还包含较小单位之间的转换(例如黑白镍币、便士、一角硬币、美元),它将很有用。

We are teaching a class where we need to teach about currency. We would like our students to add and demonstrate using different currencies across the world.

(e.g. Country=US, How much does 3 nickels + 2 pennies + 3 dimes)
(e.g. Country=UK, ...)
(e.g. Country=JAPAN, ...)
(e.g. Country=CHINA, ....)
(e.g. Country=AUSTRALIA, ...)

Is there any sample code, which demonstrates conversion of coins in different currencies?

Note: If it also contains the conversion between smaller units (e.g. b/w nickels, pennies, dimes, dollar) it would be useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万水千山粽是情ミ 2024-10-05 03:41:54

Java 枚举是一个很好的工具。您可以为每种货币创建一个:

public enum US
{
  PENNY(1),
  NICKLE(5),
  DIME(5),
  QUARTER(1);

  int value;
  US(int i)
  {
    value = i;
  }

  public int getValue()
  {
    return value;
  }
}

此代码显示了如何使用它:

public class Driver
{
  public static void main(String[] args)
  {
    System.out.println("Total value is " + (US.NICKLE.getValue() * 3 + US.QUARTER.getValue() * 2 + US.PENNY.getValue() * 4));
  }
}

Java enumerations are a nice tool. You can create one for each currency:

public enum US
{
  PENNY(1),
  NICKLE(5),
  DIME(5),
  QUARTER(1);

  int value;
  US(int i)
  {
    value = i;
  }

  public int getValue()
  {
    return value;
  }
}

This code shows how to use it:

public class Driver
{
  public static void main(String[] args)
  {
    System.out.println("Total value is " + (US.NICKLE.getValue() * 3 + US.QUARTER.getValue() * 2 + US.PENNY.getValue() * 4));
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文