使用循环Java返回给定数组中的最小数字
嗨,大家好。你好吗? =)
我是Java的新手,目前
我有一个创建方法的任务,它需要一个参数总和 - 要赠送的资金,并返回可以将此金额删除的最低钞票数。
仅在循环时才可以使用。
我做了循环,但我找不到循环时犯错的地方。 您能给我提示还是建议? 谢谢你!
public class ATM {
public static int countBanknotes(int sum) {
int[] notes = new int[] { 500, 200, 100, 50, 20, 10, 5, 2, 1 };
int[] noteCounter = new int[9];
int amount = 0;
for (int i = 0; i < 9; i++) {
if (sum >= notes[i]) {
noteCounter[i] = sum / notes[i];
sum -= noteCounter[i] * notes[i];
}
}
for (int i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
amount += noteCounter[i];
}
}
return amount;
}
public static void main(String[] args) {
// 6 (500 + 50 + 20 + 5 + 2 + 1
int sum = 578;
System.out.print(ATM.countBanknotes(sum));
}
}
和循环
public class JustATest {
public static int countBanknotes(int sum) {
int[] notes = new int[] { 500, 200, 100, 50, 20, 10, 5, 2, 1 };
int[] noteCounter = new int[9];
int amount = 0;
int i = 0;
while ( i < 9 ) {
if (sum >= notes[i]) {
i++;
noteCounter[i] = sum / notes[i];
sum -= noteCounter[i] * notes[i];
}
}
while ( i < 9 ) {
if (noteCounter[i] != 0) {
i++;
amount += noteCounter[i];
}
}
return amount;
}
public static void main(String[] args) {
// Should be 6 (500 + 50 + 20 + 5 + 2 + 1)
int sum = 578;
System.out.print(JustATest.countBanknotes(sum));
}
}
Hi guys. How are you? =)
I'm new to Java and currently
I have a task to create a method, it takes one parameter sum - the amount of money to be given out, and returns the minimum number of banknotes that can be given out this amount.
Only While loop can be used.
I made it with for loop, but I can't find where I made mistake in while loop.
Could you please give me hint or advice?
Thank you!
public class ATM {
public static int countBanknotes(int sum) {
int[] notes = new int[] { 500, 200, 100, 50, 20, 10, 5, 2, 1 };
int[] noteCounter = new int[9];
int amount = 0;
for (int i = 0; i < 9; i++) {
if (sum >= notes[i]) {
noteCounter[i] = sum / notes[i];
sum -= noteCounter[i] * notes[i];
}
}
for (int i = 0; i < 9; i++) {
if (noteCounter[i] != 0) {
amount += noteCounter[i];
}
}
return amount;
}
public static void main(String[] args) {
// 6 (500 + 50 + 20 + 5 + 2 + 1
int sum = 578;
System.out.print(ATM.countBanknotes(sum));
}
}
And while loop
public class JustATest {
public static int countBanknotes(int sum) {
int[] notes = new int[] { 500, 200, 100, 50, 20, 10, 5, 2, 1 };
int[] noteCounter = new int[9];
int amount = 0;
int i = 0;
while ( i < 9 ) {
if (sum >= notes[i]) {
i++;
noteCounter[i] = sum / notes[i];
sum -= noteCounter[i] * notes[i];
}
}
while ( i < 9 ) {
if (noteCounter[i] != 0) {
i++;
amount += noteCounter[i];
}
}
return amount;
}
public static void main(String[] args) {
// Should be 6 (500 + 50 + 20 + 5 + 2 + 1)
int sum = 578;
System.out.print(JustATest.countBanknotes(sum));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在循环之间重新初始化
i
变量,或使用其他变量,例如j
。此外,您不应在wir循环中的if语句中内部
i ++
。否则,在某些情况下,柜台永远不会增加,您将拥有无尽的循环。那太糟糕了!将您的
i ++
放在if语句之外的while循环中:这样,无论如何,计数器总是会递增,无论如何您都不会有无尽的循环。
我也将您的问题解决了,也包括在上面的代码中。
You need to reinitialize your
i
variable between the loops, or use another variable, likej
.Furthermore, you should not have your
i++
inside the if statements in your while loops. Otherwise, there are scenarios where the counter is never incremented and you will have an endless loop. That is bad!Put your
i++
in the bottom of the while loop outside the if statement like this:This way, the counters are always incremented no matter what, and you will not have endless loops.
I included a fix for your problem in the code above as well.
循环时,您需要将您的i值重新定位到第二个值
You need to reinitialize your i value to 0 for the second while loop
您必须在每次循环中执行
i ++
。否则您将进入无尽的循环。您必须重置
i
在进入下一个循环之前。因此,不建议使用一个迭代器 i 。You must execute
i++
in your while loop everytime. Otherwise you will get into endless loop.You must reset
i
before going to next loop. So one iteratori
for two loops is not recommended.