【发布时间】:2016-07-19 18:18:56
【问题描述】:
我正在使用 Visual Studio Community 2015 制作一个基于表单的程序,该程序可以掷两个骰子。我需要根据我得到的随机数更改图片。
我可以这样做的方法是:
Random num = new Random();
int dice = num.Next(1,7);
if (dice == 1) {
pictureBox.Image = proj08.Properties.Resources._1;
} else if (dice == 2) {
pictureBox.Image = proj08.Properties.Resources._2;
} else if (dice == 3) {
pictureBox.Image = proj08.Properties.Resources._3;
} else if (dice == 4) {
pictureBox.Image = proj08.Properties.Resources._4;
} else if (dice == 5) {
pictureBox.Image = proj08.Properties.Resources._5;
} else if (dice == 6) {
pictureBox.Image = proj08.Properties.Resources._6; }
这完美地工作并且做我想要的,但它是非常混乱的代码。我想通过执行以下操作来清理它:
Random num = new Random();
int dice = num.Next(1,7);
pictureBox.Image = proj08.Properties.Resources._dice;
但这不起作用。 我也想使用相同的代码,即使图片框是图片框1或图片框2,这样我就可以将它用于任何一个骰子。
【问题讨论】:
-
这个问题更多的是关于引用资源而不是骰子。至少将其标记为更合适的重复项,例如:stackoverflow.com/questions/1190729/…
标签: c#