找到最大错误:没有为max(int,int,int,int,int)找到合适的方法

时间:2016-04-11 22:31:34

标签: java

我正在试图找出为什么我在编译代码时遇到错误。它应该是找到在for循环中输入的变量的最大值和最小值。我知道代码是多余的,但它适用于一个类。

   import java.util.*;
   public class ForInputNumbers
   {
      public static void main (String[] args)
      {
      Scanner input = new Scanner(System.in);      
      int value1=0,value2=0,value3=0,value4=0,value5=0;

      System.out.println("Please type a number");

      for (; value1==0;System.out.println("Please type the next number"))
      {
      value1 = input.nextInt();
      }

      for (; value2==0;System.out.println("Please type the next number"))
      {
      value2 = input.nextInt();
      }

      for (; value3==0;System.out.println("Please type the next number"))
      {
      value3 = input.nextInt();
      }

      for (; value4==0;System.out.println("Please type the next number"))
      {
      value4 = input.nextInt();
      }

      for (; value5==0;System.out.println("Please type the next number"))
      {
      value5 = input.nextInt();
      }

   System.out.println("Your numbers: "+value1+" "+value2+" "+value3+" "+value4+" "+value5);

   System.out.println("The sum of your numbers: "+(value1+value2+value3+value4+value5));

   System.out.println("The average of your numbers: "+(value1+value2+value3+value4+value5)/5);

   System.out.println("The largest of your numbers: "+(Math.max(value1,value2,value3,value4,value5)));

   System.out.println("The smallest of your numbers: "+(Math.min(value1,value2,value3,value4,value5)));

      }//end main method
   }//end class

我的错误:

ForInputNumbers.java:60: error: no suitable method found for max(int,int,int,int,int)
   System.out.println("The largest of your numbers: "+(Math.max(value1,value2,value3,value4,value5)));
                                                           ^
    method Math.max(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(long,long) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(float,float) is not applicable
      (actual and formal argument lists differ in length)
    method Math.max(double,double) is not applicable
      (actual and formal argument lists differ in length)
ForInputNumbers.java:62: error: no suitable method found for min(int,int,int,int,int)
   System.out.println("The smallest of your numbers: "+(Math.min(value1,value2,value3,value4,value5)));
                                                            ^
    method Math.min(int,int) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(long,long) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(float,float) is not applicable
      (actual and formal argument lists differ in length)
    method Math.min(double,double) is not applicable
      (actual and formal argument lists differ in length)
2 errors

感谢任何帮助,谢谢。

3 个答案:

答案 0 :(得分:3)

Math.maxMath.min只接受参数对。您不能传递超过2个参数。

执行此操作的最佳方法是将值包装到List<Integer>中,然后使用Collections.maxCollections.min

List<Integer> values = Arrays.asList(value1, value2, value3, value4, value5);
System.out.println("The largest of your numbers: "+Collections.max(values));
System.out.println("The smallest of your numbers: "+Collections.min(values));

答案 1 :(得分:2)

Math.max()只需要2个参数,因此您必须将它们嵌套以找到最多的数字。在这种情况下,它看起来像Math.max(value1, Math.max(value2, Math.max(value3, Math.max(value4, value5))))您还必须使用Math.min()函数执行与此类似的操作。理想情况下,您可以使用数组或列表,但在这种情况下不需要。

答案 2 :(得分:0)

编辑:Andy Turner的回答可能是最好的。

原始答案:

Math.min和Math.max都期望两种数字类型,所以你需要一个接一个地调用它们,每次都保持最小值或最大值,或者最好你会创建一个接受数组的递归效用函数并返回最小值或最大值。

或者仅以一个班轮为例:

Math.max(value1, Math.max(value2, Math.max(value3, Math.max(value4, value5))));