在数组中找到最小的元素. [英] Find the smallest element in an array.

查看:76
本文介绍了在数组中找到最小的元素.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为问题出在方法或括号的调用上,而不是100%确定.当我调用该方法时,是在主方法之前还是之后有关系吗?

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?

 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;
      } 
     }
    }

推荐答案

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

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.

您的代码存在严重的格式设置问题,应始终使用IDE和IDE,例如 Eclipse ,以避免此类情况问题.
修正了以下代码:

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

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;
    }
}

这篇关于在数组中找到最小的元素.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆