数组上返回 0 的最小值(目标是找到最大值和最小值)

发布于 2024-12-20 01:33:33 字数 1716 浏览 5 评论 0原文

提前致谢。当我打印该数组的最小值时,我无法弄清楚为什么我的代码给我带来了麻烦。它似乎保留了 min 的初始 0 值,但我不知道如何纠正这个问题。麦克斯工作得很好。我是否在程序的错误部分设置了最大值和最小值?

另外,这是我的第一个数组作业,因此我可能会对稍后打印下标有一些疑问。

再次感谢!

编辑- 据我所知,这解决了问题。谢谢丹·W.

 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
        System.out.print("Enter a new number. This will continue until "
                + "you reach the size of the array. ");
        arrayOfNumbers[i] = keyboard.nextInt();
    }
            int max = arrayOfNumbers[0];
            int min = arrayOfNumbers[0];

package program8;

import java.util.Scanner;

public class arrayPrint {

    public static void main(String[] args) {
        final int NUMBER_OF_ELEMENTS;
      Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the amount of numbers the array will read in. ");
        NUMBER_OF_ELEMENTS = keyboard.nextInt();
                int arrayOfNumbers[] = new int[NUMBER_OF_ELEMENTS];
                int max = arrayOfNumbers[0];
                int min = arrayOfNumbers[0];
        for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
            System.out.print("Enter a new number. This will continue until "
                    + "you reach the size of the array. ");
            arrayOfNumbers[i] = keyboard.nextInt();
        }
        for (int i = 0; i < arrayOfNumbers.length; i++) {
            if (max <= arrayOfNumbers[i])
                max = arrayOfNumbers[i];
        }
        for (int i = 0; i < arrayOfNumbers.length; i++) {
            if (min >= arrayOfNumbers[i])
                min = arrayOfNumbers[i];
        }
                System.out.print(+max+ "max and " +min+ " min");
        }


}

Thanks in advance. I'm having trouble figuring out why my code is giving me trouble when I print a minimum value for this array. It seems to be retaining the initial 0 value for min, but I'm not sure how to correct this. Max works fine. Did I set the max and min values in the wrong part of the program?

Also, this is my first assignment with arrays so I might have some questions about printing subscripts later on.

Thanks again!

EDIT- this fixed the problem, to my knowledge. Thanks Dan W.

 for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
        System.out.print("Enter a new number. This will continue until "
                + "you reach the size of the array. ");
        arrayOfNumbers[i] = keyboard.nextInt();
    }
            int max = arrayOfNumbers[0];
            int min = arrayOfNumbers[0];

package program8;

import java.util.Scanner;

public class arrayPrint {

    public static void main(String[] args) {
        final int NUMBER_OF_ELEMENTS;
      Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the amount of numbers the array will read in. ");
        NUMBER_OF_ELEMENTS = keyboard.nextInt();
                int arrayOfNumbers[] = new int[NUMBER_OF_ELEMENTS];
                int max = arrayOfNumbers[0];
                int min = arrayOfNumbers[0];
        for (int i = 0; i < NUMBER_OF_ELEMENTS; i++) {
            System.out.print("Enter a new number. This will continue until "
                    + "you reach the size of the array. ");
            arrayOfNumbers[i] = keyboard.nextInt();
        }
        for (int i = 0; i < arrayOfNumbers.length; i++) {
            if (max <= arrayOfNumbers[i])
                max = arrayOfNumbers[i];
        }
        for (int i = 0; i < arrayOfNumbers.length; i++) {
            if (min >= arrayOfNumbers[i])
                min = arrayOfNumbers[i];
        }
                System.out.print(+max+ "max and " +min+ " min");
        }


}

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

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

发布评论

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

评论(1

那伤。 2024-12-27 01:33:33

您输入什么值?因为int的默认值为0:这里
因此,如果您只输入正数,则最小值将始终为 0。

为避免这种情况,请在用户输入数字后初始化最小值和最大值,这样最大值和最小值就不会默认为 0。

What values are you inputting? Because an int has a default value of 0: here
So, if you're only inputting positive numbers, the min will always be 0.

To avoid this, initialize min and max after you have the user input the numbers, that way the max and min are not defaulted to 0.

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