使用循环Java返回给定数组中的最小数字

发布于 2025-01-24 06:30:32 字数 1830 浏览 2 评论 0原文

嗨,大家好。你好吗? =)

我是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 技术交流群。

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

发布评论

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

评论(3

怪我鬧 2025-01-31 06:30:32

您需要在循环之间重新初始化i变量,或使用其他变量,例如j

此外,您不应在wir循环中的if语句中内部i ++。否则,在某些情况下,柜台永远不会增加,您将拥有无尽的循环。那太糟糕了!
将您的i ++放在if语句之外的while循环中:

while ( i < 9 ) {
    if (sum >= notes[i]) {
        noteCounter[i] = sum / notes[i];
        sum -= noteCounter[i] * notes[i];           
    }
    i++;
}
int j = 0;
while ( j < 9 ) {           
    if (noteCounter[j] != 0) {
        amount += noteCounter[j];           
    }
    j++;
}

这样,无论如何,计数器总是会递增,无论如何您都不会有无尽的循环。
我也将您的问题解决了,也包括在上面的代码中。

You need to reinitialize your i variable between the loops, or use another variable, like j.

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:

while ( i < 9 ) {
    if (sum >= notes[i]) {
        noteCounter[i] = sum / notes[i];
        sum -= noteCounter[i] * notes[i];           
    }
    i++;
}
int j = 0;
while ( j < 9 ) {           
    if (noteCounter[j] != 0) {
        amount += noteCounter[j];           
    }
    j++;
}

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.

帝王念 2025-01-31 06:30:32

循环时,您需要将您的i值重新定位到第二个值

You need to reinitialize your i value to 0 for the second while loop

自由如风 2025-01-31 06:30:32
  1. 您必须在每次循环中执行i ++。否则您将进入无尽的循环。

  2. 您必须重置i在进入下一个循环之前。因此,不建议使用一个迭代器 i 。

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]) {
            noteCounter[i] = sum / notes[i];
            sum -= noteCounter[i] * notes[i];
        }
        i++;
    }

    i = 0;
    while (i < 9) {
        if (noteCounter[i] != 0) {
            amount += noteCounter[i];
        }
        i++;
    }
    return amount;
}
  1. You must execute i++ in your while loop everytime. Otherwise you will get into endless loop.

  2. You must reset i before going to next loop. So one iterator i for two loops is not recommended.

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]) {
            noteCounter[i] = sum / notes[i];
            sum -= noteCounter[i] * notes[i];
        }
        i++;
    }

    i = 0;
    while (i < 9) {
        if (noteCounter[i] != 0) {
            amount += noteCounter[i];
        }
        i++;
    }
    return amount;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文