如何保持for循环计数器为0;大十进制; ETC

发布于 2024-12-22 19:48:00 字数 761 浏览 3 评论 0原文

我一直在努力完成我的大学的以下作业。到目前为止,我已经多次收到关于这项任务的帮助(我真的很感激)。

由于这是大学的作业,我希望能有非直接的答案,用不能直接解决我的作业的例子来解释概念。

该作业要求我根据用户输入检查一些内容。

  1. 如果用户输入订单,然后再次使用相同的代码输入相同的订单,则会用新的信息替换以前的信息。 EG,如果用户输入 G22 作为代码,5 作为数量,然后再次输入 G22,然后输入 4 作为数量,它会忘记 5 并将其替换为 4。我怎样才能使它记住前一个并简单地添加新订单并使其数量 = 9(我使用数组进行用户输入)。

  2. 如果用户输入 G22 和数量 3,但没有输入任何内容,只需按 Enter 键,计数器就会添加 1,然后按 X(退出),运费将显示为 2.00 美元,即 2 件商品。如果没有输入任何合理的内容,即输入了除了 4 个可用选项之外的任何其他内容,如何避免计数器加 1。

  3. 我知道对于与金钱相关的任何事情都不建议使用双精度数,建议使用 BigDecimal(也许还有 NumberFormat)。我可以用 BigDecimal 替换当前的双精度数吗?还是必须替换整个代码?我也不明白如何轻松地实现 BigDecimal,因此我希望获得外行示例(特别是关于我是否可以用整数加/减/乘/除 BigDecimal 等)。

  4. 我会在这里发布代码,但我不希望其他人做同样的课程复制它,然后在我之前提交它(并不是说我的代码是世界上最棒的东西,我只是放入了一个自从这是我的第一个程序以来,我付出了很多努力)。

I have been trying to complete the following assignment for my college. So far, I have received help on this assignment a couple of times (which I really appreciate).

Since this is an assignment for college, I would appreciate non-direct answers that explain the concepts with examples that don't directly fix my assignment.

The assignment requires me to check for some things depending on user input.

  1. If the user inputs an order, then inputs the same order with the same code again, it replaces the previous information with the new one. EG, if the user inputs G22 as code and 5 as quantity, then again enters G22 and then 4 as quantity, it forgets about the 5 and replaces that with 4. How can I make it so it remembers the previous one and simply adds the new order and makes it Quantity = 9 (I am using arrays for user input).

  2. If the user enters G22 and quantity 3, but then doesn't enter anything and just hits enter, the counter adds one and upon pressing X (to quit) the shipping charges show up as $2.00 which is for 2 items. How can I avoid the counter from adding 1 in case nothing sensible is entered, i.e. anything else apart from the 4 options available is entered.

  3. I understand that using doubles is not advisable for anything related to money and BigDecimal is recommended (alongwith NumberFormat, maybe). Can I replace my current doubles with BigDecimal with minimal problems, or do I have to replace the whole code? I also do not understand how to implement BigDecimal that easily so I would appreciate layman examples on that (especially on whether I can add/subtract/multiply/divide BigDecimal with ints etc).

  4. I would post the code here but I don't want it copied by anyone else doing the same course and then submit it before I do (not that my code is the most awesome thing in the world, I just put in a lot of effort since its my first ever program).

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

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

发布评论

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

评论(1

霊感 2024-12-29 19:48:00

1) 我将使用 Map 来存储产品代码的所有数量。

2)我将通过对地图中的数量求和来获得订购的物品总数。用户如何输入数据并不重要。

3) 此建议取决于项目。在你的情况下,标记的选项才是最重要的。在投资银行和贸易公司工作了很多年,我还没有看到有人用BigDecimal来赚钱。它不是主要的重写,至少您应该知道如何使用 double 和 BigDecimal 正确处理舍入。

1) I would use a Map<String, Integer> to store all the quantities for a product code.

2) I would get the total of items ordered by summing the quantities in the map. How the user enters the data shouldn't matter.

3) This advice is project dependant. In your case the option of the marker is what matters. Working in investment banks and trading firms for many years, and I haven't seen anyone use BigDecimal for money. Its not a major rewrite, and at a minimum you should know how to use both double and BigDecimal handling rounding correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文