Created
April 11, 2016 22:32
-
-
Save nesheep5/d0a592427a68c0d99a7a301b0d441cf3 to your computer and use it in GitHub Desktop.
FizzBuzz.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // パターン1 | |
| for(x <- 1 to 100) x match { | |
| case x if x % 3 == 0 && x % 5 == 0 => println("FizzBuzz") | |
| case x if x % 3 == 0 => println("Fizz") | |
| case x if x % 5 == 0 => println("Buzz") | |
| case _ => println(x) | |
| } | |
| // パターン2 | |
| for(x <- 1 to 100) (x % 3, x % 5) match { | |
| case (0,0) => println("FizzBuzz") | |
| case (0,_) => println("Fizz") | |
| case (_,0) => println("Buzz") | |
| case _ => println(x) | |
| } | |
| // パターン3 | |
| def fizzbuzz(x:Int )= { | |
| (x%3,x%5) match { | |
| case (0,0) => "FizzBuzz" | |
| case (0,_) => "Fizz" | |
| case (_,0) => "Buzz" | |
| case _ => x | |
| } | |
| } | |
| (1 to 100).map(fizzbuzz).foreach(println) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment