Scala Math.max未选取最大数

Scala Math.max未选取最大数,scala,Scala,尝试编写一个简单的scala程序,将用户输入作为int,存储在元组中,然后从元组中选择最大值。我不知道为什么我的代码不起作用 import scala.io.StdIn._ println("Please enter four numbers.") val one = readInt() val two = readInt() val three = readInt() val four = readInt() val numbers = (one, two, three, four) prin

尝试编写一个简单的scala程序,将用户输入作为int,存储在元组中,然后从元组中选择最大值。我不知道为什么我的代码不起作用

import scala.io.StdIn._
println("Please enter four numbers.")
val one = readInt()
val two = readInt()
val three = readInt()
val four = readInt()
val numbers = (one, two, three, four)
println(math.max(numbers))
我得到的错误是:

C:\Users\Tyler\Documents\School\CSC10101\Mimir Assignments\max.scala:8: error: overloaded method value max with alternatives:
  (x: Double,y: Double)Double <and>
  (x: Float,y: Float)Float <and>
  (x: Long,y: Long)Long <and>
  (x: Int,y: Int)Int
 cannot be applied to ((Int, Int, Int, Int))
println(math.max(numbers))
             ^
one error found
C:\Users\Tyler\Documents\School\CSC10101\Mimir Assignments\max.scala:8:错误:重载了方法值max,并提供了备选方案:
(x:Double,y:Double)Double
(x:浮动,y:浮动)浮动
(x:Long,y:Long)Long
(x:Int,y:Int)Int
无法应用于((Int,Int,Int,Int))
println(数学最大值(数字))
^
发现一个错误

非常感谢您的帮助

math.max
只能应用于2个参数-您有4个参数。如果你有四个数字,你可以做的是:

math.max(math.max(math.max(one, two), three), four)
按照@Javier在下面评论中提出的建议,如果您的号码是在
Seq
或其他集合中收集的,您可以应用
reduce
高阶函数:

List(one, two, three, four).reduce(math.max)
或者更好:

List(one, two, three, four).max

您的问题的示例代码

import scala.io.StdIn._
val numbers = for (_ <- 0 until 4) yield readInt()
val maxNumber = numbers.reduce(math.max)
println(maxNumber)
导入scala.io.StdIn_

val numbers=for(u)这将是一个使用reduce:Tanjin的好地方,谢谢你的回答。你知道为什么不能使用元组吗?@Tyler,在回答你的问题时:与集合不同,每个元组配置都有自己的独立类型。If
Math.max()
如果采用元组,则必须对
(Int,Int)
(Int,Int,Int)
(Int,Int,Int,Int)
,等等,以及
(Long,Long)
(Long,Long,Long)
,以及
(Float,Float)
,等等都有一个不同的定义.max)
以防您想将其推广到没有提供
列表的操作中
-内置:
println(List(List(one,two,three,four).reduce(math.max))
。只是
List.max
List.min
List.sum
对我来说总是那么不自然。。。