• 追加された行はこの色です。
• 削除された行はこの色です。
```*Scala解答例 [#p9ba346f]

[[練習問題]]

-Scala 2.10.2
-Java 1.7.0_21 32bit

ほとんどの解答例は[[ideone.com:http://ideone.com/]]でそのまま実行できます

**[[ループ練習>練習問題#eb2c4338]] [#s1c6b422]
***問1 [#ke7969de]
object Main extends App {
for (i <- 1 to 5) {
println("Hello World!")
}
}

object Main extends App {
(1 to 5) foreach { _ =>
println("Hello World!")
}
}

***問2 [#ybb0b896]
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)
}
}

**[[FizzBuzz>練習問題#t52e5a48]] [#ga808233]
object Main extends App {
def fizzBuzz(n: Int) = {
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)
}

**[[素数判定>練習問題#o8db2119]] [#v8e35b34]
**[[素数判定>練習問題#o8db2119]] [#ldc71efe]
***判定 [#v8e35b34]
object Main extends App {
def prime(n: Int) = 2 <= n && !(2 until (math.sqrt(n) + 1.0).toInt).exists(n % _ == 0)
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 "非素数"))
}

***列挙 [#j53b2078]
object Main extends App {
def primes(xs: Stream[Int] = Stream.from(2)): Stream[Int] =

primes() takeWhile {
_ <= 100
} foreach {
println
}
}

**[[うるう年測定>練習問題#i0d67516]] [#xd158eb1]
import java.util.GregorianCalendar

object Main extends App {
val cal = new GregorianCalendar()

while (true) {
print("西暦を入力してください> ")