2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

何かのお題を抽選するプログラムをC#で実装しました

ソースコード

using System;
// リストを使用するクラスを呼び出す
using System.Collections.Generic;
using System.Linq;
// ファイルの読み書きのためのライブラリを呼び出す
using System.IO;
using System.Text;

namespace Sample
{
    class Sample
    {
        static void Main()
        {   
            // ファイル名を定義
            string file_name = "sakamichi.txt";
            // EIDリストの定義
            var titlelist = new List<string>();
            // StreamReaderクラスを読み込むファイル名とエンコードを指定してインスタンス化する
            StreamReader sr = new StreamReader(file_name, Encoding.GetEncoding("utf-8"));
            // 配列の要素の0番目を指定する変数
            int target = 0;
            while (sr.Peek() != -1)
            {
                // ファイルを1行ずつ読み込み、リストに追加する
                string eid = sr.ReadLine();
                titlelist.Add(eid);
            }

            sr.Close();
            // お題リストの要素をシャッフルする
            titlelist = titlelist.OrderBy(a => Guid.NewGuid()).ToList();

            Console.WriteLine("-----乃木坂に関するお題を1つ表示します-----");
            Console.WriteLine(titlelist[target]);
            
           
        }
    }
}

お題ファイル

好きな乃木坂46の曲
好きな櫻坂46の曲
好きな日向坂46の曲
乃木坂46の推しメン
櫻坂46の推しメン
日向坂46の推しメン

出力結果

test1.jpg

最後に

C#5はWindowsに標準でインストールされるコンパイラーなので、C#5でプログラムを始めてはいかがでしょうか

2
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?