Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Created September 10, 2014 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petitviolet/6cfecff5a97104a7bd31 to your computer and use it in GitHub Desktop.
Save petitviolet/6cfecff5a97104a7bd31 to your computer and use it in GitHub Desktop.
scala_fizzbuzz
object fizzbuzz {
def fizzbuzz(n: Int): Unit = {
}
def main(args: Array[String]) = {
val n: Int = args(0).toInt
for (i <- 1 to n){
i match {
case i if i % 15 == 0 => println("fizzbuzz")
case i if i % 3 == 0 => println("fizz")
case i if i % 5 == 0 => println("buzz")
case _ => println(i)
}
}
}
}
object fizzbuzz1 {
def main(args: Array[String]) = {
val n: Int = args(0).toInt
(1 to n).foreach {i =>
var output = ""
if (i % 3 == 0) {
output += "fizz"
}
if (i % 5 == 0) {
output += "buzz"
}
println(output match {
case "" => i
case _ => output
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment