Java 查找最大错误:找不到适合max的方法(int,int,int,int,int)

Java 查找最大错误:找不到适合max的方法(int,int,int,int,int),java,Java,我试图找出编译代码时出错的原因。它应该查找在for循环中输入的变量的最大值和最小值。我知道代码是多余的,但它是为一个类编写的 import java.util.*; public class ForInputNumbers { public static void main (String[] args) { Scanner input = new Scanner(System.in); int value1=0,va

我试图找出编译代码时出错的原因。它应该查找在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

感谢您的帮助。

编辑:安迪·特纳的回答可能是最好的

原始答复:

Math.min和Math.max都需要两种数字类型,因此您需要一个接一个地调用它们,每次都保持最小值或最大值,或者最好创建一个接受数组并返回最小值或最大值的递归实用程序函数

或者对于单衬里,仅作为示例:

Math.max(value1, Math.max(value2, Math.max(value3, Math.max(value4, value5))));
max只接受2个参数,因此必须嵌套它们才能找到更多数字的最大值。在本例中,它看起来像Math.maxvalue1、Math.maxvalue2、Math.maxvalue3、Math.maxvalue4、value5。您还必须使用Math.min函数执行类似的操作。理想情况下,您可以使用数组或列表,但在这种情况下不需要。Math.max和Math.min只接受成对的参数。传递的参数不能超过2个

最简单的方法是将值包装到列表中,然后使用Collections.max和Collections.min:


请在发布前阅读错误消息for循环的意义是什么?@natecat我提到它是用于赋值的,它是多余的等等。一般来说,java风格指南建议您在每个逗号后面加空格。@natecat公平地说,他们也会说,你绝对不应该做这种仅限于这个用例的事情。理想情况下,你应该有一个数组并减少它。但是他没有一个数组,而查找不在一个数组中的超过2个数字的最大值的习惯用法是嵌套函数。我只是建议你编辑你的帖子并加入whitespace@Natecat我绝对不同意这种说法,像这样的嵌套函数只会使代码不可读。安迪·特纳的回答恰到好处。@NeilTwist,不一定是最好的。它干净多了。然而,也许这不是老师想要的,这看起来像是一个作业问题。@NeilTwist-事实上,分号是不正确的。这是一个表达式,不是一个语句。从语法上讲,它可以是一个语句,但在上下文中,计算最多4个数字,然后将结果扔掉是没有意义的。即使是断章取义。。。国际海事组织@NeilTwist-哪种代码?问题中的代码或答案中的代码。如果你在谈论答案,我不同意。除非您使用最佳实践作为您喜欢的代码的速记。。。很多人都这么做:-@NeilTwist asker已经将值的数量硬编码为5,而且这个项目非常小,不需要重用代码。即使这是一个大型项目,如果您曾经使用过这样的函数,您也不需要更改值的数量。像这样的代码可能会引入什么讨厌的bug?@NeilTwist-你为什么这么说?也许他有。但无论如何,决定硬连线方法是否正确的不是代码库的大小。真正的决定因素是你可以硬连线的假设是否正确。老实说,除非我们看到OP老师给出的要求,否则我们不能做出这样的判断。@NeilTwist-请记住这里的上下文。这显然是一个初学者做的编程练习。目的是不写优雅的,生产质量代码。目的是写一个简单的程序。。。让它工作起来。您试图教授的这些课程可能超出了新手程序员的理解能力。没有不尊重的意思!谢谢,我真的很喜欢这个。
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));