LoginSignup
0
0

More than 1 year has passed since last update.

多言語FizzBuzzチャレンジ7日目:C#

Last updated at Posted at 2022-12-06

これまでのまとめ

本日のお品書き

適当に名前で並べたのでC, C++の次ですが、はい、全く違う言語に突入します。C#です。
書きっぷりがオブジェクト(コンポーネント)指向感あふれてきますが、FizzBuzz程度だと片鱗が見えるぐらいですね。

FizzBuzz

using System;

class Hello {
  static void Main() {
    for (int i = 1; i < 101; i++) {
      if (i % 15 == 0) {
        Console.WriteLine("FizzBuzz");
      } else if (i % 3 == 0) {
        Console.WriteLine("Fizz");
      } else if (i % 5 == 0) {
        Console.WriteLine("Buzz");
      } else {
        Console.WriteLine(i);
      }
    }
  }
}

コンパイルにあたってはVisual Studioを導入するのが王道のようですが、上記程度のプログラムであればWindowsのどこかに入っているcsc.exeを使えばよいですね。(C:\Windows\Microsoft.NET以下あたり)

csc main.cs

別解: dotnetを使った作成

簡単なコンソールアプリケーションであれば、dotnetを使ってプロジェクトを作成することで、ロジック部分だけに注目してプログラムを書けるようです。

# wingetなどで.NET Framework 6.0あたりを導入しておく
dotnet new console -o fizzbuzz -f net6.0
cd fizzbuzz
# edit Program.cs
dotnet run
Program.cs
for (int i = 1; i < 101; i++) {
  if (i % 15 == 0) {
    Console.WriteLine("Fizz Buzz");
  } else if (i % 3 == 0) {
    Console.WriteLine("Fizz");
  } else if (i % 5 == 0) {
    Console.WriteLine("Buzz");
  } else {
    Console.WriteLine(i);
  }
}
0
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
0
0