0

在 main 方法中读取一个包含 5 个整数值的数组。创建一个单独的函数来确定哪个数字最大。将最大值返回给 main 并在 main 中打印。我开始编写代码,但我的问题是我不知道如何将我在 main 方法中读取的值(数组值)传递给函数!我是这样开始的:

import java.util.*;
public class arraybiggest {
    public static int main (String[]args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 5 values: ");
        int [] x = new int[5];
        for (int i=0; i<5; i++) {
            x[i] = in.nextInt();
            return x;
        }
    }
    public static int  passarray(int [ ] value) {

    }
}

请帮助我!谢谢!

4

4 回答 4

1

您可以调用您的函数并将数组传递给它,如下所示:

passarray(x);

但首先你需要return x;从循环中删除。

于 2013-09-05T13:16:27.203 回答
0
import java.util.*;
public class arraybiggest {

   public static int main (String[]args) {
          Scanner in = new Scanner(System.in);
          System.out.println("Enter 5 values: ");
          int [] x = new int[5];
          for (int i=0; i<5; i++) {
             x[i] = in.nextInt();           
         }

         int result = passarray(x);
         System.out.println("the biggest number is:" +result );
   }


  public static int  passarray(int [ ] value) {
        int result=value[0]
        for(int i=1;i<value.length;i++){
            if (value[i] > result) {
                result= value[i];
            }
        }
       return result;
   }
}
于 2013-09-05T13:17:56.657 回答
0
for (int i=0; i<5; i++) {
        x[i] = in.nextInt();
        }
 passarray(x);
于 2013-09-05T13:18:58.820 回答
0
    import java.util.*;
    public class arraybiggest {
        public static int main (String[]args) {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter 5 values: ");
            int [] x = new int[5];
            for (int i =0; i<5; i++) {
                x[i] = in.nextInt();
            }
            System.out.println(findMax(x));
        }
        public static int  findMax(int [ ] value) {
            int max = value[0];
            for (int i = 1; i < value.length; i++) {
                if (value[i] > max) {
                    max = value[i];
                }
            }
            return max;
        }

    }
于 2013-09-05T13:19:37.783 回答