Java 找出数组中最小的元素。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20465001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the smallest element in an array.
提问by user3081791
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% 肯定。当我调用该方法时,它是在 main 方法之前还是之后重要吗?
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;
}
}
}
采纳答案by Infinite Recursion
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 Eclipseto 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;
}
}
回答by BobTheBuilder
You cannotdeclare a method inside another one.
您不能在另一个方法中声明一个方法。
In your code you try to declare double min(double[] array)
inside your main
method.
在您的代码中,您尝试double min(double[] array)
在main
方法中声明。