解答例は次の環境でテストしています
ほとんどの解答例はideone.comでそのまま実行できます
object Main extends App { for (i <- 1 to 5) { println("Hello World!") } }
別解
object Main extends App { (1 to 5) foreach { _ => println("Hello World!") } }
object Main extends App { val j = args(0).toInt for (i <- 1 to j) { println("Hello World! " + i) } }
別解
object Main extends App { val j = args(0).toInt (1 to j) foreach { i => println("Hello World! " + i) } }
object Main extends App { def fizzBuzz(n: Int) = { if (n % 15 == 0) { "FizzBuzz" } else if (n % 5 == 0) { "Buzz" } else if (n % 3 == 0) { "Fizz" } else { n.toString } } (1 to 100).map(fizzBuzz).foreach(println) }
object Main extends App { def prime(n: Int) = 2 <= n && !(2 until (math.sqrt(n) + 1.0).toInt).exists(n % _ == 0) val n = args(0).toInt println(n.toString + "は" + (if (prime(n)) "素数" else "非素数")) }