从列表中随机选取一个数字后删除它

时间:2015-04-22 19:12:22

标签: c#

我制作了一个小型宾果游戏,我有一个数字列表(1-90)。我可以随机选择一个数字,但是在我随机选择它之后如何删除它?

这是我到目前为止的尝试:

public class CloseFriends {
    // Contains edges represented as an incident matrix
    private static boolean edges[][];

    // Adds a directed edge between v1 and v2
    // The method sorts the edges to reduce space used 
    public static void addEdge(int v1, int v2) {
        if (v1 > v2) {
            edges[v1][v2] = true;
        }
        else {
            edges[v2][v1] = true;
        }
    }

    // Creates a graph with V vertices
    public static void createVertices(int V) {
        edges = new boolean[V][V];
    }

    // Checks if an edge exists between v1 and v2
    public static boolean isFriends(int v1, int v2) {
        if (v1 > v2) {
            return edges[v1][v2];
        }
        else {
            return edges[v2][v1];
        }
    }

    // Checks if an ArrayList of vertices is close friends
    public static void closeFriends(ArrayList<Integer> vertices) {
        int count = 0;
        int size = vertices.size();

        for (int i = 0; i < size; i++) {
            for (int j = i + 1; j < size; j++) {
                if (isFriends(vertices.get(i), vertices.get(j))) {
                    count++;
                }
            }
        }
        // The clique should contain n*(n-1)/2 edges for everyone to be connected
        if (count == (size * (size - 1) / 2)) {
            System.out.println("yes");
        }
        else {
            System.out.println("no");
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(in.readLine());

        // Count vertices, and create that amount
        int V = 0;
        while (st.hasMoreTokens()) {
            st.nextToken();
            V++;
        }
        createVertices(V);

        ArrayList<Integer> friends = new ArrayList<Integer>();

        // While System.in has something to be read
        while (in.ready()) {
            // Read the line and add edges based on input, or run the algorithm
            String edge = "";

            edge = in.readLine();

            if (edge != null) {
                st = new StringTokenizer(edge);
                if (!edge.startsWith("closefriends")) {
                    addEdge(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()));
                }
                else {
                    st.nextToken();
                    while (st.hasMoreTokens()) {
                        friends.add(Integer.parseInt(st.nextToken()));
                    }
                }
            }
        }
        closeFriends(friends);
    }
}

1 个答案:

答案 0 :(得分:2)

每次点击该按钮,您的列表都会重新填充所有90个号码。我想每次点击它应该包含少一个数字,是吗?

然后你需要在点击方法之外实例化它。

List<int> BingoNumbers = Enumerable.Range(1, 90).ToList();

private void btn_Number_Click(object sender, EventArgs e)
{
    int r = BingoNumbers.OrderBy(bn => Guid.NewGuid()).FirstOrDefault();
    BingoNumbers.Remove(r);
    RichTextBox1.Text = BingoNumbers[r].ToString();
    RichTextBox2.Text = BingoNumbers[r].ToString();
}

我还删除了Random变量,并将其替换为一些LINQ魔法,它通过随机因子命令您剩余的宾果游戏数字并获取第一个参数 - 并将其删除。 如果Bingonumbers为空,则只返回0.

如果你想实现真正的洗牌,你可以选择Fisher-Yates-Shuffle:

Random rnd = new Random();
//Fisher-Yates-shuffle
for (int x = BingoNumbers.Count - 1; x > 0; x--)
{
    int y = rnd.Next(x + 1);
    //Swapping 
    int temp = BingoNumbers[x];
    BingoNumbers[x] = BingoNumbers[y];
    BingoNumbers[y] = temp;
}
//take the first item out of the shuffled list
int r = BingoNumbers[0];
//remove item
BingoNumbers.Remove(r);