2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DartでFizzBuzz

Posted at

Install

インストール方法は公式にあるのでそちらを参照。
https://www.dartlang.org/install
当方はMacなので、Homebrewでインストールする形。

brew tap dart-lang/dart
brew install dart

コード

app.dart
const start = 1;
const stop  = 100;

fizzBuzz(int num, [int fizz = 3, int buzz = 5]) {
  if (num % fizz == 0 && num % buzz == 0) {
    return "FizzBuzz";
  } else if (num % fizz == 0) {
    return "Fizz";
  } else if (num % buzz == 0) {
    return "Buzz";
  } else {
    return num;
  }
}

main(List<String> args) {
  for (var i = start; i <= stop; i++) {
    var result = fizzBuzz(i);
    print("${i} is ${result}");
  }
}

実行

dart app.dart

結果

1 is 1
2 is 2
3 is Fizz
4 is 4
5 is Buzz
6 is Fizz
7 is 7
8 is 8
9 is Fizz
10 is Buzz
11 is 11
12 is Fizz
13 is 13
14 is 14
15 is FizzBuzz
16 is 16
17 is 17
18 is Fizz
19 is 19
20 is Buzz
21 is Fizz
22 is 22
23 is 23
24 is Fizz
25 is Buzz
26 is 26
27 is Fizz
28 is 28
29 is 29
30 is FizzBuzz
31 is 31
32 is 32
33 is Fizz
34 is 34
35 is Buzz
36 is Fizz
37 is 37
38 is 38
39 is Fizz
40 is Buzz
41 is 41
42 is Fizz
43 is 43
44 is 44
45 is FizzBuzz
46 is 46
47 is 47
48 is Fizz
49 is 49
50 is Buzz
51 is Fizz
52 is 52
53 is 53
54 is Fizz
55 is Buzz
56 is 56
57 is Fizz
58 is 58
59 is 59
60 is FizzBuzz
61 is 61
62 is 62
63 is Fizz
64 is 64
65 is Buzz
66 is Fizz
67 is 67
68 is 68
69 is Fizz
70 is Buzz
71 is 71
72 is Fizz
73 is 73
74 is 74
75 is FizzBuzz
76 is 76
77 is 77
78 is Fizz
79 is 79
80 is Buzz
81 is Fizz
82 is 82
83 is 83
84 is Fizz
85 is Buzz
86 is 86
87 is Fizz
88 is 88
89 is 89
90 is FizzBuzz
91 is 91
92 is 92
93 is Fizz
94 is 94
95 is Buzz
96 is Fizz
97 is 97
98 is 98
99 is Fizz
100 is Buzz
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?