C# 掷骰子游戏
它的掷骰子应用程序。我想总结骰子结果,并将其呈现给用户。目前,骰子图像将改变后,我点击按钮“点击滚动模具” 但是,当我掷骰子1时,结果将不会是加法(+0),而当我掷骰子2时,结果将仅为(+1)。我不知道我的代码有什么问题:C# 掷骰子游戏,c#,arrays,dice,C#,Arrays,Dice,它的掷骰子应用程序。我想总结骰子结果,并将其呈现给用户。目前,骰子图像将改变后,我点击按钮“点击滚动模具” 但是,当我掷骰子1时,结果将不会是加法(+0),而当我掷骰子2时,结果将仅为(+1)。我不知道我的代码有什么问题: public partial class PigForm : Form { Image[] diceImages; int[] dice; Random roll; private void rollDieBotton_Click(objec
public partial class PigForm : Form
{
Image[] diceImages;
int[] dice;
Random roll;
private void rollDieBotton_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
for (int i = 0; i < dice.Length; i++)
{
var currentRoll = roll.Next(0, 6);
dice[i] += currentRoll;
dicePictureBox.Image = diceImages[currentRoll];
playersTotal.Text = String.Format("{0}", dice[i]);
}
}
private void PigForm_Load(object sender, EventArgs e)
{
diceImages = new Image[6];
diceImages[0] = Properties.Resources.Alea_1;
diceImages[1] = Properties.Resources.Alea_2;
diceImages[2] = Properties.Resources.Alea_3;
diceImages[3] = Properties.Resources.Alea_4;
diceImages[4] = Properties.Resources.Alea_5;
diceImages[5] = Properties.Resources.Alea_6;
dice = new int[1] { 0 };
roll = new Random();
}
}
公共部分类PigForm:Form
{
图像图像;
int[]骰子;
随机滚动;
私有无效RollDieboton_单击(对象发送者,事件参数e)
{
滚动骰子();
}
私有void RollDice()
{
for(int i=0;i
这将生成一个从0到5(含)的随机数。您可能希望从1到6生成:
var currentRoll = roll.Next(1, 7)
参考:
编辑:
dicePictureBox.Image = diceImages[currentRoll - 1]
这将生成一个从0到5(含)的随机数。您可能希望从1到6生成:
var currentRoll = roll.Next(1, 7)
参考:
编辑:
dicePictureBox.Image = diceImages[currentRoll - 1]
您允许
currentRoll
变量在[0,6]
之间。这包括0
,但不包括6
。您可能应该更改为var currentRoll=roll.Next(1,7)代码>
编辑注释:然后为了访问数组值(索引为零),应该从滚动结果中减去一 您允许currentRoll
变量在[0,6]
之间。这包括0
,但不包括6
。您可能应该更改为var currentRoll=roll.Next(1,7)代码>
编辑注释:然后为了访问数组值(索引为零),应该从滚动结果中减去一 关于您的代码的几点评论:
public partial class PigForm : Form
{
Image[] diceImages;
int dice;
Random roll;
private void rollDieBotton_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
var currentRoll = roll.Next(1, 7);
dice += currentRoll;
dicePictureBox.Image = diceImages[currentRoll-1];
playersTotal.Text = String.Format("{0}", dice);
}
private void PigForm_Load(object sender, EventArgs e)
{
diceImages = new Image[6];
diceImages[0] = Properties.Resources.Alea_1;
diceImages[1] = Properties.Resources.Alea_2;
diceImages[2] = Properties.Resources.Alea_3;
diceImages[3] = Properties.Resources.Alea_4;
diceImages[4] = Properties.Resources.Alea_5;
diceImages[5] = Properties.Resources.Alea_6;
dice = 0;
roll = new Random();
}
}
- 如果数组总是包含一个整数,为什么要使用它?这也使得for循环非常无用。使用纯整数并删除循环
Random
类的Next()
方法有两个参数。第一个是包含下界,第二个是独占上界。这意味着在您的情况下,0将是一个可能的数字,而6将永远不会出现。(MSDN第页:)
下面是对您的代码的轻微修改:
public partial class PigForm : Form
{
Image[] diceImages;
int dice;
Random roll;
private void rollDieBotton_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
var currentRoll = roll.Next(1, 7);
dice += currentRoll;
dicePictureBox.Image = diceImages[currentRoll-1];
playersTotal.Text = String.Format("{0}", dice);
}
private void PigForm_Load(object sender, EventArgs e)
{
diceImages = new Image[6];
diceImages[0] = Properties.Resources.Alea_1;
diceImages[1] = Properties.Resources.Alea_2;
diceImages[2] = Properties.Resources.Alea_3;
diceImages[3] = Properties.Resources.Alea_4;
diceImages[4] = Properties.Resources.Alea_5;
diceImages[5] = Properties.Resources.Alea_6;
dice = 0;
roll = new Random();
}
}
关于代码的几点意见:
public partial class PigForm : Form
{
Image[] diceImages;
int dice;
Random roll;
private void rollDieBotton_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
var currentRoll = roll.Next(1, 7);
dice += currentRoll;
dicePictureBox.Image = diceImages[currentRoll-1];
playersTotal.Text = String.Format("{0}", dice);
}
private void PigForm_Load(object sender, EventArgs e)
{
diceImages = new Image[6];
diceImages[0] = Properties.Resources.Alea_1;
diceImages[1] = Properties.Resources.Alea_2;
diceImages[2] = Properties.Resources.Alea_3;
diceImages[3] = Properties.Resources.Alea_4;
diceImages[4] = Properties.Resources.Alea_5;
diceImages[5] = Properties.Resources.Alea_6;
dice = 0;
roll = new Random();
}
}
- 如果数组总是包含一个整数,为什么要使用它?这也使得for循环非常无用。使用纯整数并删除循环
Random
类的Next()
方法有两个参数。第一个是包含下界,第二个是独占上界。这意味着在您的情况下,0将是一个可能的数字,而6将永远不会出现。(MSDN第页:)
下面是对您的代码的轻微修改:
public partial class PigForm : Form
{
Image[] diceImages;
int dice;
Random roll;
private void rollDieBotton_Click(object sender, EventArgs e)
{
RollDice();
}
private void RollDice()
{
var currentRoll = roll.Next(1, 7);
dice += currentRoll;
dicePictureBox.Image = diceImages[currentRoll-1];
playersTotal.Text = String.Format("{0}", dice);
}
private void PigForm_Load(object sender, EventArgs e)
{
diceImages = new Image[6];
diceImages[0] = Properties.Resources.Alea_1;
diceImages[1] = Properties.Resources.Alea_2;
diceImages[2] = Properties.Resources.Alea_3;
diceImages[3] = Properties.Resources.Alea_4;
diceImages[4] = Properties.Resources.Alea_5;
diceImages[5] = Properties.Resources.Alea_6;
dice = 0;
roll = new Random();
}
}
- 首先,为骰子设置一个整数数组是没有意义的,因为您只需要一个数字,这也意味着您不需要为它设置一个循环,因为它永远不会重复第二次
- 其次,你的随机数从0到5,你希望它从1到6
您只需对代码进行一些编辑:
var currentRoll = roll.Next(1, 7);
dice = currentRoll; // there should not be += operator because the result of the next roll will be absurd
dicePictureBox.Image = diceImages[currentRoll - 1]; // -1 because your array is zero-based which means that it starts from 0
这就是随机类的工作原理。在您的例子中,初始值是1,但结尾的值不是,所以您需要的是1,7,因为它将返回一个包含在1和6之间的数字。
- 首先,为骰子设置一个整数数组是没有意义的,因为您只需要一个数字,这也意味着您不需要为它设置一个循环,因为它永远不会重复第二次
- 其次,你的随机数从0到5,你希望它从1到6
您只需对代码进行一些编辑:
var currentRoll = roll.Next(1, 7);
dice = currentRoll; // there should not be += operator because the result of the next roll will be absurd
dicePictureBox.Image = diceImages[currentRoll - 1]; // -1 because your array is zero-based which means that it starts from 0
这就是随机类的工作原理。在您的例子中,初始值是1,但结尾的值不是,所以您需要的是1,7,因为它将返回一个包含在1和6之间的数字。正如其他人所指出的,Random。接下来(a,b)
在a包含和b排除之间生成一个随机数
虽然这很容易做到
var currentRoll = roll.Next(1, 7);
这将断开阵列访问线,稍后将有两条线
相反,您最好的选择是修改加法行
dice[i] += currentRoll + 1;
正如其他人指出的,Random.Next(a,b)
在a包含和b排除之间生成一个随机数
虽然这很容易做到
var currentRoll = roll.Next(1, 7);
这将断开阵列访问线,稍后将有两条线
相反,您最好的选择是修改加法行
dice[i] += currentRoll + 1;
请看Random.Next(int,int)方法的文档:
在那里,你会发现下限是包含的,上限是独占的。
因此,Next(0,6)表示得到0、1、2、3、4或5。请查看Random的文档。Next(int,int)方法:
在那里,你会发现下限是包含的,上限是独占的。
因此,Next(0,6)表示得到0、1、2、3、4或5。我不太明白您的问题是什么,但我确实发现了一些看起来不对劲的地方
尝试更改:
dice[i] += currentRoll;
与:
我不太明白你的问题是什么,但我确实发现了一些看起来不对劲的地方
尝试更改:
dice[i] += currentRoll;
与:
我修改了我的密码