c# - 每次按下按钮时,如何按顺序保存旧的数字变量?
问题描述
我试图从随机数生成器中排除旧结果,但我似乎无法让变量记住过去的两个。我是 C# 新手,所以我可能忽略了一些非常简单的解决方案。如果你们能启发我,那将是非常有帮助的。
我已经尝试过下面显示的代码,我很确定我很愚蠢,但我不知道为什么它不起作用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label_click(object sender, EventArgs e)
{
Close();
}
int mouseX = 0, mouseY = 0;
bool mouseDown;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void GOBUTN_Paint(object sender, PaintEventArgs e)
{
}
//Sharing is caring: Communism goes here
private System.Windows.Forms.Timer timtim;
PictureBox Rooskie = new PictureBox();
int duplicheck = 0;
int duplicheck2 = 0;
int duplicheck3 = 0;
Label test = new Label();
private void boopthesnoot(object sender, EventArgs e)
{
dinging:
//yeah, random numbers here
Random rando = new Random();
int rand0 = rando.Next(1, 25);
test.Text = duplicheck + ", " + duplicheck2 + ", " + duplicheck3;
test.Font = new Font("Calibri", 20);
Controls.Add(test);
test.Location = new Point(0, 200);
test.Height = 1000;
test.Width = 1000;
if(duplicheck != rand0 && duplicheck2 != rand0 && duplicheck3 != rand0)
{
GOBUTTON.Hide();
pictureBox1.Hide();
pictureBox2.Hide();
//image code goes here
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "resources" + "\\" + rand0 + ".jpg";
Rooskie.Width = 1160;
Rooskie.Height = 620;
Bitmap image = new Bitmap(filepath);
Rooskie.Dock = DockStyle.Fill;
Rooskie.Image = (Image)image;
Controls.Add(Rooskie);
Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;
//Aww, it's that timer time
timtim = new System.Windows.Forms.Timer();
timtim.Tick += new EventHandler(clockfinish);
timtim.Interval = 5000;
timtim.Start();
}
else
{
goto dinging;
}
if (duplicheck != rand0)
{
duplicheck2 = duplicheck;
}
if(duplicheck != rand0)
{
duplicheck2 = duplicheck;
}
if (duplicheck2 != duplicheck)
{
duplicheck3 = duplicheck2;
}
duplicheck = rand0;
}
private void clockfinish(object sender, EventArgs e)
{
//CEASE THE TIMER AND GIVE ME BACK MY BUTTON
Rooskie.Image = null;
timtim.Stop();
GOBUTTON.Show();
pictureBox1.Show();
pictureBox2.Show();
}
这个想法是随机数生成器选择图像并显示它。但我不希望同一图像连续显示多次,所以这应该给它一个缓冲区,即如果第一次按下按钮时显示图像 2,则图像 1 显示在第二个按钮上,图像 3 是显示在第三个,结果应该是这样的: duplicheck = 2 duplicheck2 = 1 duplicheck3 = 3
解决方案
因此,在评论中每个人的帮助和大量的混乱下,我最终解决了这个问题。它可以完美地防止任何图像在每 25 次单击按钮时显示超过一次。非常感谢你们在这方面的帮助!如果没有你,我想我不会想到这一点。
//Sharing is caring: Communism goes here
private System.Windows.Forms.Timer timtim;
int rand0;
PictureBox Rooskie = new PictureBox();
Label test = new Label();
Random rando = new Random();
List<int> duplicheck = new List<int>();
private void boopthesnoot(object sender, EventArgs e)
{
dingding:
//yeah, random numbers here
rand0 = rando.Next(1, 26);
/*string combowombo = string.Join(", ", duplicheck.ToArray());
test.Text = combowombo;
test.Font = new Font("Calibri", 20);
Controls.Add(test);
test.Location = new Point(0, 200);
test.Height = 1000;
test.Width = 1000;*/
if(duplicheck.Contains(rand0))
{
goto dingding;
}
else
{
GOBUTTON.Hide();
pictureBox1.Hide();
pictureBox2.Hide();
//image code goes here
Rooskie.Width = 1160;
Rooskie.Height = 620;
Bitmap image = new Bitmap(WindowsFormsApp1.Properties.Resources._1);
Rooskie.Dock = DockStyle.Fill;
Rooskie.Image = (Image)image;
Controls.Add(Rooskie);
Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;
//Aww, it's that timer time
timtim = new System.Windows.Forms.Timer();
timtim.Tick += new EventHandler(clockfinish);
timtim.Interval = 3000;
timtim.Start();
duplicheck.Add(rand0);
if (duplicheck.Count == 25)
{
duplicheck = new List<int>();
}
}
}
推荐阅读
- jackson - 使用 RestTemplate.postForObject(.....) 将 rest 服务的字符串响应的 xml 列表自动映射到相应的 java 对象
- python - python错误不确定是错误还是代码问题
- c++ - 如何减少 Lambda 过多的堆栈使用
- javascript - AngularJS 中的语音识别解决方案
- c - 使用 C 预处理器实现 if-else
- solr - 使用 solr 端点创建 CloudSolrClient
- java - 返回值分配问题(对于原始数据类型)
- java-8 - 在 java 8 中将 Collection(java.util.Collection) 转换为 Observable(io.reactivex.Observable)
- python - 如何对列表中的数据进行排序?
- linkedin - 我从 LinkedIn 登录获得的身份验证令牌无效