硬币柜台的镍币没有显示正确的金额(Nickles in Coin Counter not displaying correct amount)

编程入门 行业动态 更新时间:2024-10-16 02:27:09
硬币柜台的镍币没有显示正确的金额(Nickles in Coin Counter not displaying correct amount)

我刚刚在Java课程中完成了第一次任务,我们的任务是创建一个计算Nickles并显示总金额的程序。 然而,每当我把一个奇数的镍币,它不显示金额的金额。 例如,1个镍成为$ .5,而不是$ .05。 21尼克斯变成1.5美元而不是1.05美元。 我喜欢这是一个容易解决的简单问题,但我发现自己难倒了。 谢谢您的帮助。

public static void main(String[] args) { Scanner in = new Scanner(System.in); int nickles; System.out.println("Deposit your Nickles."); nickles = in.nextInt(); int nickles5 = nickles * 5; int dollars = nickles5 / 100; int change = nickles5 % 100; System.out.println("You have $" + dollars + "." + change); }

I was just given my first assignment in my Java class, and we were tasked with creating a program that counts nickles and displays the total amount of money. However, whenever I put an odd number of nickles it does not display the corrent amount of money. For example, 1 nickles turns into $.5 instead of $.05. 21 Nickles turns into $1.5 instead of $1.05. I'm asumming this is an easy fix for an easy problem, but I am finding myself stumped. Thanks for the help.

public static void main(String[] args) { Scanner in = new Scanner(System.in); int nickles; System.out.println("Deposit your Nickles."); nickles = in.nextInt(); int nickles5 = nickles * 5; int dollars = nickles5 / 100; int change = nickles5 % 100; System.out.println("You have $" + dollars + "." + change); }

最满意答案

我会在这里出去,试图解决你的问题。 看起来你会要求用户输入一些数量的镍币,并且你会以美元输出金额。 请让我知道这是否以任何方式不正确。

现在看下面的代码。

public static void main(String[] args) { Scanner in = new Scanner(System.in); int nickles; System.out.println("Deposit your Nickles."); nickles = in.nextInt(); double nickles5 = nickles * 0.05; System.out.println("You have $" + nickles5); }

在存储了一个int类型的镍元素之后,我们将它乘以0.05,以得到所有镍元素的实际值。 我们把这个存储在一个double变量中[注:int乘以double,返回double; 它被称为数字促销]。 现在,要获得总值,您可以简单地打印这个双变量。 在n = 2的情况下,镍5 = 0.1。 所以,它会打印0.1美元

如果您希望显示为0.10美元,只需用String.format( "%.2f", nickels5)替换nickels5即可String.format( "%.2f", nickels5)

现在你的最后一行将如下所示:

System.out.println("You have $" + String.format( "%.2f", nickels5));

让我知道这是否解决了你的问题。

I'll go out on a limb here, to try to solve your problem. It appears that you will ask the user to enter some number of nickels, and you will output the amount in dollars. Please let me know if this is incorrect in any way.

Now look at the following code.

public static void main(String[] args) { Scanner in = new Scanner(System.in); int nickles; System.out.println("Deposit your Nickles."); nickles = in.nextInt(); double nickles5 = nickles * 0.05; System.out.println("You have $" + nickles5); }

After we have the number of nickels stored in an int, we multiply it by 0.05, to get the actual value of all the nickels. We store this in a double variable [Note: int multiplied to a double, returns a double; it is called numeric promotion]. Now, to get the total value, you can simply print this double variable. In a case where n=2, nickels5 = 0.1. So, it will print $0.1

If you want this to show up as $0.10, simply replace the nickels5 with String.format( "%.2f", nickels5)

Now your final line will look like:

System.out.println("You have $" + String.format( "%.2f", nickels5));

Let me know if this solved your issue.

更多推荐

本文发布于:2023-08-04 00:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405045.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:镍币   硬币   柜台   金额   正确

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!