-1

特定の範囲内の数字を「n」回シャッフルする簡単なプログラム(宝くじジェネレーター)を作成したいと思います。シャッフルするたびに、1つの乱数を選択し、指定された範囲のリストから新しい番号に移動します。リストし、これを「n」回実行します(特定の数の数値を選択するまで、正確には7回)。私はまさにそれを行うアルゴリズムを見つけました(拡張メソッドまたはジェネリックリストのシャッフル)。しかし、私はプログラミングにそれほど興味がなく、結果(描画された数値のリスト)をTextBoxまたはLabelに表示するのに問題がありますが、MessageBoxで動作するようになっています。しかし、TextBox / Labelを使用すると、「名前*は現在のコンテキストに存在しません」というエラーが発生します。私は解決策をグーグルで検索しましたが、これまで何の助けもありませんでした。

コードは次のとおりです。

      private void button1_Click(object sender, EventArgs e)
      {
         List<int> numbers;
         numbers = Enumerable.Range(1, 39).ToList();
         numbers.Shuffle();
      }

      private void brojevi_TextChanged(object sender, EventArgs e)
      {          
      }
  }
}

/// <summary>
/// Class for shuffling lists
/// </summary>
/// <typeparam name="T">The type of list to shuffle</typeparam>
public static class ListShufflerExtensionMethods
{
    //for getting random values
    private static Random _rnd = new Random();

    /// <summary>
    /// Shuffles the contents of a list
    /// </summary>
    /// <typeparam name="T">The type of the list to sort</typeparam>
    /// <param name="listToShuffle">The list to shuffle</param>
    /// <param name="numberOfTimesToShuffle">How many times to shuffle the list,    by default this is 5 times</param>
    public static void Shuffle<T>(this List<T> listToShuffle, int numberOfTimesToShuffle = 7)
    {          
        //make a new list of the wanted type
        List<T> newList = new List<T>();

        //for each time we want to shuffle
        for (int i = 0; i < numberOfTimesToShuffle; i++)
        {
            //while there are still items in our list
            while (listToShuffle.Count >= 33)
            {
                //get a random number within the list
                int index = _rnd.Next(listToShuffle.Count);

                //add the item at that position to the new list
                newList.Add(listToShuffle[index]);

                //and remove it from the old list
                listToShuffle.RemoveAt(index);
            }

            //then copy all the items back in the old list again
            listToShuffle.AddRange(newList);

            //display contents of a list
            string line = string.Join(",", newList.ToArray());
            brojevi.Text = line;

            //and clear the new list
            //to make ready for next shuffling
            newList.Clear();
            break;
        }
    }
}

}

4

2 に答える 2

1

ListShufflerExtensionMethodsは、範囲外であるため、テキストボックス(brojevi)を認識しません。再構築してShuffleに値を返すようにしてから、呼び出し元のスコープでテキストボックスのテキストの値を設定できます。

于 2013-02-27T20:51:29.053 に答える
1

問題は、brojeviTextBoxまたはLabel)が拡張メソッドのスコープで定義されていないことです。Controlしたがって、で定義する必要がありますForm。したがって、番号をシャッフルするときは、イベントハンドラーTextBoxの実行中に番号を入れてくださいbutton1_Click

行を削除します。

    string line = string.Join(",", newList.ToArray());
    brojevi.Text = line;

編集:

このように拡張メソッドを変更して、描画されたアイテムの文字列または描画されたアイテムのリストを返すことができます。あなたが他のことに数字を使いたいかもしれないので、リストに行きましょう。また、最後のシャッフルしか見えないので、7回シャッフルしても意味がわかりません。したがって、1つで十分だと思います。コードを確認してください:

public static List<T> Shuffle<T>(this List<T> listToShuffle)
        {
            //make a new list of the wanted type
            List<T> newList = new List<T>();


            //while there are still items in our list
            while (listToShuffle.Count >= 33)
            {
                //get a random number within the list
                int index = _rnd.Next(listToShuffle.Count);

                //add the item at that position to the new list
                newList.Add(listToShuffle[index]);

                //and remove it from the old list
                listToShuffle.RemoveAt(index);
            }

            //then copy all the items back in the old list again
            listToShuffle.AddRange(newList);

            return newList;
        }

そして、button1_Click1イベントハンドラーでは、次のことができます。

List<int> numbers;
numbers = Enumerable.Range(1, 39).ToList();
List<int> drawnNumbers = numbers.Shuffle();
string line = string.Join(",", drawnNumbers.ToArray());
brojevi.Text = line;
于 2013-02-27T20:55:34.023 に答える