C# 生成赢得'的随机数;我不会再出现了,号码不详

C# 生成赢得'的随机数;我不会再出现了,号码不详,c#,random,numbers,repeat,C#,Random,Numbers,Repeat,您好,我写了一个程序,显示文件夹中的任何图片,在.exe中。 它包含一个“随机”选择另一张图片并显示的按钮。 到目前为止,它起了作用 接下来我想做的是,按钮选择一张以前没有选择过的图片。只有当每一张图片都显示出来时,程序才会重置并重新开始显示所有图片 但当我现在启动程序时,它显示一张图片,就是这样。这个按钮没有做任何可见的事情,你不能改变第一张图片-我不知道为什么。 (我看到了类似的问题,比如21点主题。但就我而言,我不知道会有多少张图片,所以我无法创建随机数字的固定池,并将它们从列表或类似的内

您好,我写了一个程序,显示文件夹中的任何图片,在.exe中。 它包含一个“随机”选择另一张图片并显示的按钮。 到目前为止,它起了作用

接下来我想做的是,按钮选择一张以前没有选择过的图片。只有当每一张图片都显示出来时,程序才会重置并重新开始显示所有图片

但当我现在启动程序时,它显示一张图片,就是这样。这个按钮没有做任何可见的事情,你不能改变第一张图片-我不知道为什么。 (我看到了类似的问题,比如21点主题。但就我而言,我不知道会有多少张图片,所以我无法创建随机数字的固定池,并将它们从列表或类似的内容中删除)

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace randompix
{
    public partial class Form1 : Form
    {

        int i = 0;
        Random rnd = new Random();
        string pfad = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        IEnumerable<string> allebilder; // Will contain every filename of the folder
        List<int> wdhlist = new List<int>(); //will contain 'i' that was already used



        public Form1()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            pBox1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
            pBox1.SizeMode = PictureBoxSizeMode.Zoom;
            btn2.Anchor = (AnchorStyles.Top | AnchorStyles.Right);

            allebilder = Directory.EnumerateFiles(pfad); 
            wdhlist.Add(i);

            if (allebilder.ElementAt<string>(i).EndsWith(".jpg") || allebilder.ElementAt<string>(i).EndsWith(".jpeg") || allebilder.ElementAt<string>(i).EndsWith(".png") || allebilder.ElementAt<string>(i).EndsWith(".gif") || allebilder.ElementAt<string>(i).EndsWith(".bmp"))
            {
                pBox1.ImageLocation = allebilder.ElementAt<string>(i);
                label1.Text = allebilder.ElementAt<string>(i);
            }

            else 
            {
                if (i == 0) // because 0 is already added 
                {
                    i = rnd.Next(allebilder.Count<string>());
                }
                else
                {
                    wdhlist.Add(i);
                    i = rnd.Next(allebilder.Count<string>());
                }
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            i = rnd.Next(allebilder.Count<string>());
            wdhlist.Add(i);

            if (wdhlist.Count() >= allebilder.Count<string>())
            {
                wdhlist.Clear();
            }

            else
            {
                if (wdhlist.Contains(i))
                {
                    i = rnd.Next(allebilder.Count<string>());
                    wdhlist.Add(i);
                }

                else
                {
                    if (allebilder.ElementAt<string>(i).EndsWith(".jpg") || allebilder.ElementAt<string>(i).EndsWith(".jpeg") || allebilder.ElementAt<string>(i).EndsWith(".png") || allebilder.ElementAt<string>(i).EndsWith(".gif") || allebilder.ElementAt<string>(i).EndsWith(".bmp"))
                    {
                        if (allebilder.ElementAt<string>(i) == null)
                        {
                            MessageBox.Show("Nope");
                        }

                        else
                        {
                            label1.Text = allebilder.ElementAt<string>(i);
                            pBox1.ImageLocation = allebilder.ElementAt<string>(i);
                        }
                    }
                    else
                    {
                        i = rnd.Next(allebilder.Count<string>());
                        wdhlist.Add(i);
                    }
                }
            }
        }

        private void btn1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn1_Click(sender, e);
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Version 0.3\n\nUse: Copy .exe in a folder with pictures\nSupports .jpg, .gif, .png and .bmp\n\nProgram by Fabian Schmidt\nIcon by Aleksandra Wolska\n(https://www.iconfinder.com/iconsets/49handdrawing)");
            btn1.Focus();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.IO;
使用System.Linq;
运用系统反思;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间随机化PIX
{
公共部分类Form1:Form
{
int i=0;
随机rnd=新随机();
字符串pfad=Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
IEnumerable Allebider;//将包含文件夹的每个文件名
List wdhlist=new List();//将包含已使用的“i”
公共表格1()
{
初始化组件();
this.WindowState=FormWindowState.Maximized;
pBox1.Anchor=(AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
pBox1.SizeMode=PictureBoxSizeMode.Zoom;
btn2.Anchor=(AnchorStyles.Top | AnchorStyles.Right);
Allebider=目录.EnumerateFiles(pfad);
wdhlist.Add(i);
if(allebilder.ElementAt(i).EndsWith(“.jpg”)| allebilder.ElementAt(i).EndsWith(.jpeg”)| allebilder.ElementAt(i).EndsWith(.png”)| allebilder.ElementAt(i).EndsWith(“.gif”)| allebilder.ElementAt(i).EndsWith(.bmp”))
{
pBox1.ImageLocation=allebiler.ElementAt(i);
label1.Text=allebileder.ElementAt(i);
}
其他的
{
如果(i==0)//因为已经添加了0
{
i=rnd.Next(allebilder.Count());
}
其他的
{
wdhlist.Add(i);
i=rnd.Next(allebilder.Count());
}
}
}
私有无效btn1_单击(对象发送方,事件参数e)
{
i=rnd.Next(allebilder.Count());
wdhlist.Add(i);
如果(wdhlist.Count()>=allebilder.Count())
{
wdhlist.Clear();
}
其他的
{
if(wdhlist.Contains(i))
{
i=rnd.Next(allebilder.Count());
wdhlist.Add(i);
}
其他的
{
if(allebilder.ElementAt(i).EndsWith(“.jpg”)| allebilder.ElementAt(i).EndsWith(.jpeg”)| allebilder.ElementAt(i).EndsWith(.png”)| allebilder.ElementAt(i).EndsWith(“.gif”)| allebilder.ElementAt(i).EndsWith(.bmp”))
{
if(allebileder.ElementAt(i)==null)
{
MessageBox.Show(“Nope”);
}
其他的
{
label1.Text=allebileder.ElementAt(i);
pBox1.ImageLocation=allebiler.ElementAt(i);
}
}
其他的
{
i=rnd.Next(allebilder.Count());
wdhlist.Add(i);
}
}
}
}
私有void btn1u KeyDown(对象发送方,KeyEventArgs e)
{
如果(e.KeyCode==Keys.Enter)
{
btn1_点击(发送者,e);
}
}
私有无效btn2_单击(对象发送者,事件参数e)
{
MessageBox.Show(“版本0.3\n\n使用:在包含图片的文件夹中复制.exe\n支持.jpg、.gif、.png和.bmp\n\n Fabian Schmidt的程序\n Aleksandra Wolska的nIcon\n(https://www.iconfinder.com/iconsets/49handdrawing)");
btn1.Focus();
}
}
}

您需要形成图像文件名列表。然后形成一个索引列表-从0到列表中该图像的数量

List<int> numberList = Enumerable.Range(0, imagesList.Count).ToList();
使用后,将其从numberList中删除

DoSomeThingWithImage(imagesList[randomValue]);
numberList.Remove(randomValue);
所以你会有不重复的随机图像,直到它们都不出现。然后再重复一遍。
希望这会有所帮助。

您可以创建一个随机顺序的图像序列,然后在完整使用该列表时重新创建一个序列

private Random rnd = new Random();
private IEnumerable<string> GetImagesInRandomOrder(){
    return Directory.EnumerateFiles(pfad).OrderBy(x => rnd.Next());
}

private IEnumerator<string> randomImages = 
          Enumerable.Empty<string>.GetEnumerator();

private string GetNextImage(){
       if(!randomImages.MoveNext()){
          randomImages = GetImagesInRandomOrder().GetEnumerator();
          if(!randomImages.MoveNext()){ 
              throw new InvalidOperationException("No images"); 
          }   
       }
       return randomImages.Current;
}
private Random rnd = new Random();
private IEnumerable<string> GetImagesInRandomOrder(){
    return Directory.EnumerateFiles(pfad).OrderBy(x => rnd.Next());
}

private IEnumerator<string> randomImages = 
          Enumerable.Empty<string>.GetEnumerator();

private string GetNextImage(){
       if(!randomImages.MoveNext()){
          randomImages = GetImagesInRandomOrder().GetEnumerator();
          if(!randomImages.MoveNext()){ 
              throw new InvalidOperationException("No images"); 
          }   
       }
       return randomImages.Current;
}
DoSomeThingWithImage(GetNextImage());