2011-10-26 5 views
4
object TestClass { 
    def main (args: Array[String]) { 
    println("Hello World"); 
    val c = List (1,2,3,4,5,6,7,8,9,10) 
    println(findMax(c)) 
    } 
    def findMax (tempratures: List[Int]) { 
    tempratures.foldLeft(Integer.MIN_VALUE) {Math.max} 
    } 
} 

표시 출력 이유는 출력되지인쇄 값

Hello World 
10 

내가

답변

10

이이 중 하나 인 IntelliJ이하고 있어요는

Hello World 
() 

입니다 가장 일반적인 스칼라 오타.

당신은 당신의 방법의 끝에서 =을 놓치고 :

def findMax (tempratures: List[Int]) { 

읽어야합니다

def findMax (tempratures: List[Int]) = { 

= 오프를 떠나 당신의 방법은 Unit (아무것도) 반환하지 않습니다 것을 의미한다.

+0

가 지금은 dummie 같은 느낌 싶어요. 그래서'='는이 메소드가 무언가를 리턴한다는 것을 의미합니까? – Omnipresent

+4

@Omnipresent : 모든 메소드가 뭔가를 반환하지만'= '가 없으면 반환 유형은 항상'Unit' 즉,()입니다. 스칼라는 함수형 언어로 설계되었으며, 함수형 프로그래밍에서는 각 if 구문이 거의 'if'와 같은 값을 반환합니다. 'val x = if b {ifVal} {elseVal}'. – ffriend

7

반환 유형이없는 findMax을 정의하므로 반환 유형은 Unit 또는 ()입니다.

def findMax (tempratures: List[Int]) { ... } 

def findMax (tempratures: List[Int]) : Unit = { ... } 

일명 당신은 :) 대신

def findMax (tempratures: List[Int]) : Int = { ... } 

또는 생략 형으로

def findMax (tempratures: List[Int]) = { ... }