找到数组中的最小元素。

时间:2021-05-21 21:43:29

I think the problem lies with the invoking of the method or the braces, not 100% sure. When I call the method does it matter if it before or after the main method?

我认为问题在于调用方法或大括号,而不是100%肯定。当我调用方法时,它在主方法之前或之后是否重要?

 public class varb
    {
    public static void main (String[] args)
    {   
    double[] array = new double [10]; 
    java.util.Scanner input = new java.util.Scanner(System.in); 
    System.out.println("Enter" + " " + array.length + " numbers");
    for (int c = 0;c<array.length;c++)
    {
    array[c] = input.nextDouble();
    }
    min(array);
    double min(double[] array)
    {
    int i;
    double min = array[0];
    for(i = 1; i < array.length; i++)
     {
    if(min > array[i])
      {
    min = array[i];
      }
     }
    return min;
      } 
     }
    }

2 个解决方案

#1


3  

The location of main does not matter, it can be placed anywhere in the class, generally convention is to place it as the first method or last method in the class.

main的位置无关紧要,它可以放在类的任何位置,通常约定是将它作为类中的第一个方法或最后一个方法。

You code has severe formatting problems, you should always use and IDE, like Eclipse to avoid such issues.
Fixed your code below:

您的代码有严重的格式问题,您应该始终使用IDE和Eclipse一样避免此类问题。修正了以下代码:

public class Varb{
    public static void main(String[] args) {

        double[] array = new double[10];
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.println("Enter" + " " + array.length + " numbers");
        for (int c = 0; c < array.length; c++) {
            array[c] = input.nextDouble();
        }
        min(array);
    }

    private static double min(double[] array) {
        double min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }
}

#2


1  

You cannot declare a method inside another one.

你不能在另一个方法中声明一个方法。

In your code you try to declare double min(double[] array) inside your main method.

在您的代码中,您尝试在main方法中声明double min(double [] array)。

#1


3  

The location of main does not matter, it can be placed anywhere in the class, generally convention is to place it as the first method or last method in the class.

main的位置无关紧要,它可以放在类的任何位置,通常约定是将它作为类中的第一个方法或最后一个方法。

You code has severe formatting problems, you should always use and IDE, like Eclipse to avoid such issues.
Fixed your code below:

您的代码有严重的格式问题,您应该始终使用IDE和Eclipse一样避免此类问题。修正了以下代码:

public class Varb{
    public static void main(String[] args) {

        double[] array = new double[10];
        java.util.Scanner input = new java.util.Scanner(System.in);
        System.out.println("Enter" + " " + array.length + " numbers");
        for (int c = 0; c < array.length; c++) {
            array[c] = input.nextDouble();
        }
        min(array);
    }

    private static double min(double[] array) {
        double min = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }
}

#2


1  

You cannot declare a method inside another one.

你不能在另一个方法中声明一个方法。

In your code you try to declare double min(double[] array) inside your main method.

在您的代码中,您尝试在main方法中声明double min(double [] array)。