开发者

scala simple while loop error

object JDWhileLoop
{               
                def main(args: Array[String])

                {
                        var index:Int = 0
                        while( index<=10)
                        {

                        println("index="+index)
                        index= index+1              

                        }

                }       
}                   

here is the error

JDWhileLoop.scala:3: error: only classes can have declared but undefined members def main(args: Array[S开发者_运维知识库tring]) ^

I got this simple code and try to make work,but is not ,I dont know why.please help me. thanks


That's formatting error. This should be fine:

object JDWhileLoop
{               
                def main(args: Array[String]) 
                {
                        var index:Int = 0
                        while( index<=10) {
                        println("index="+index)
                        index= index+1           
                        }
                }       
} 

In your code def main(args: Array[String]) treated as an abstract method (without body) followed by some code block in object inner body definition.

Note that in scala the following braces style is preffered

def foo (args: Bar) {
  //some work 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜