嘿,每个人我在一个项目上遇到麻烦,一个尝试块。我需要执行以下操作: 第一次检查是否掷出第一骰,如果为真,则跳过保持检查并掷出所有骰子 如果为假,请尝试以下操作(“锁定”新持有的骰子(以某种方式)并滚动未持有的骰子) 如果没有骰子,则抛出异常我已附上我的代码。我很迷失,有人可以帮忙...。public partial class FrmBupkis1 : Form{ private PictureBox[] diceImages; private CheckBox[] holds; private Random rnd = new Random(); public Frm1() { InitializeComponent(); diceImages = new PictureBox[6]; diceImages[0] = pbxDie0; diceImages[1] = pbxDie1; diceImages[2] = pbxDie2; diceImages[3] = pbxDie3; diceImages[4] = pbxDie4; diceImages[5] = pbxDie5; holds = new CheckBox[6]; holds[0] = chbHold0; holds[1] = chbHold1; holds[2] = chbHold2; holds[3] = chbHold3; holds[4] = chbHold4; holds[5] = chbHold5; } private void rollBtn_Click(object sender, EventArgs e) { //First Check for first roll, if true skip hold checks and roll all dice //If false, Try the following ( 'lock' newly held dice (somehow) AND roll unheld dice ) //If no dice are held, throw exception for (int i = 0; i < 6; i++) { //if die is not held, then assign random number to image box if (holds[i].Checked == false) diceImages[i].Image = iglDice.Images[rnd.Next(6)]; try { Random = random.Next(0, 6); diceImages[i].Image = dieImages[Random]; rollBtn = true; } catch (FormatException ex) { Console.WriteLine(ex.Message, "Error. Try Again."); } } } private void gameOverBtn_Click(object sender, EventArgs e) { for (int i = 0; i < 6; i++) { diceImages[i].Image = null; holds[i].Checked = false; holds[i].Enabled = true; } } private void quitBtn_Click(object sender, EventArgs e) { //btnQuit this.Close(); }} 最佳答案 这段代码有很多问题:Random = random.Next(0, 6);diceImages[i].Image = dieImages[Random];假设dieImages应该确实是diceImages(否则将无法编译)您正在使用与.NET类同名的变量-令人困惑。另外,似乎没有在任何地方声明此变量-只需直接使用全局定义的Random实例即可:diceImages[i].Image = diceImages[rnd.Next(6)];另外,您使用的范围是0到6-使用单参数方法Next来获得范围从0到5(上限是排他的),如上所示,否则,当您访问。关于c# - try catch 掷骰子的问题,以及复选框问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5061076/ 10-10 13:22