Java程序不会打印出数组中最小的数字

时间:2018-10-10 05:39:57

标签: java arrays

我正尝试在我的array中找到最大和最小的数字,但是,它似乎只打印出最大的数字,而不是最小的数字,我是一个很新的人,感谢所有帮助。

import java.util.Scanner;

public class Ex1 {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        int[] numbers = new int[11];

        int largest = numbers[0];
        int smallest = numbers[0];

        System.out.println("Enter 10 numbers = ");

        for (int i = 1; i < numbers.length; i++) {
            numbers[i] = keyboard.nextInt();
            System.out.println("Number " + i + " = " + numbers[i]);

        for (int y = 1; y < numbers.length; y++) {
            if (numbers[y] > largest)
            {
                largest = numbers[y];
            }
        }

        for (int x = 1; x < numbers.length; x++)    
        {
            if (numbers[x] < smallest)
            {
                smallest = numbers[x];
            }
        }   

        }

            System.out.println("Largest number = " + largest);
            System.out.println("Smallest number = " + smallest);




    } // main

} // class

4 个答案:

答案 0 :(得分:2)

它不会输出最小的数字,因为您将其开头设置为0,而是将其设置为Integer.MAX_VALUE

还要确保您的程序适用于负数,请将largest的值设置为Integer.MIN_VALUE

答案 1 :(得分:0)

 int smallest = numbers[0];

这正在产生问题。这会在开始时分配值0,因此您不会得到最小的数字。

您的测试数据集还必须仅包含positive numbers

解决方案

   int largest = Integer.MIN_VALUE;
    int smallest = Integer.MAX_VALUE;

答案 2 :(得分:0)

除了正确找到输入数字的最小值和最大值之外,还有一些代码问题。

您不必为此编写嵌套的for循环。检查下面的代码,这将简单地为您找到最小和最大数字,

    Scanner keyboard = new Scanner(System.in);
    int[] numbers = new int[11];

    int largest = numbers[0];
    int smallest = numbers[0];

    System.out.println("Enter 10 numbers = ");

    for (int i = 1; i < numbers.length; i++) {
        numbers[i] = keyboard.nextInt();
        System.out.println("Number " + i + " = " + numbers[i]);
        if (i == 1) {
            largest = numbers[i];
            smallest = numbers[i];
        }
        if (numbers[i] > largest) {
            largest = numbers[i];
        }
        if (numbers[i] < smallest) {
            smallest = numbers[i];
        }

    }

    System.out.println("Largest number = " + largest);
    System.out.println("Smallest number = " + smallest);

答案 3 :(得分:0)

尝试一下

    public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
             int[] numbers = new int[10];

                System.out.println("Enter 10 numbers = ");

                for (int i = 0; i < numbers.length; i++) {
                    numbers[i] = keyboard.nextInt();
                    System.out.println("Number " + (i + 1) + " = " + numbers[i]);
                }
                int largest = numbers[0];
                int smallest = numbers[0];

                for (int y = 0; y < numbers.length; y++) {
                    if (numbers[y] >= largest)
                    {
                        largest = numbers[y];
                    }

                    if (numbers[y] <= smallest)
                    {
                        smallest = numbers[y];
                    }
                }
                    System.out.println("Largest number = " + largest);
                    System.out.println("Smallest number = " + smallest);
   }