我正在尝试制作一个骰子游戏,但是我的骰子变量存在问题,并且跟踪当前一轮的得分。
基本上,有四个骰子,圆形分数是加在一起的骰子的总数(每轮你有一定数量的重卷)。
但是当我尝试使用游戏时,圆形分数只显示一个骰子的数字,无论哪一个最近滚动(点击)。
这是我的代码:
namespace DiceGame
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int die1;
int die2;
int die3;
int die4;
int rollsRemaining = 6;
int scoreRound;
int scoreTotal;
int numberRound;
Random random;
Random rnd = new Random();
public MainWindow()
{
InitializeComponent();
Random random = new Random();
int die1 = random.Next(1, 7);
int die2 = random.Next(1, 7);
int die3 = random.Next(1, 7);
int die4 = random.Next(1, 7);
dieOne.Content = die1;
dieTwo.Content = die2;
dieThree.Content = die3;
dieFour.Content = die4;
int scoreRound = die1 + die2 + die3 + die4;
roundScore.Text = "Round Score: " + scoreRound;
}
private void dieOne_Click(object sender, RoutedEventArgs e)
{
Random random = new Random();
if (rollsRemaining != 0)
{
int die1 = random.Next(1, 7);
dieOne.Content = die1;
rollsRemaining = rollsRemaining - 1;
string string1 = rollsRemaining.ToString();
rollsLeft.Text = "Rolls Left: " + string1;
roundScore.Text = "Round Score: " + (die1 + die2 + die3 +die4);
}
else
{ }
}
private void dieTwo_Click(object sender, RoutedEventArgs e)
{
Random random = new Random();
if (rollsRemaining != 0)
{
int die2 = random.Next(1, 7);
dieTwo.Content = die2;
rollsRemaining = rollsRemaining - 1;
string string1 = rollsRemaining.ToString();
rollsLeft.Text = "Rolls Left: " + string1;
roundScore.Text = "Round Score: " + (die1 + die2 + die3 + die4);
}
else
{ }
}
private void dieThree_Click(object sender, RoutedEventArgs e)
{
Random random = new Random();
if (rollsRemaining != 0)
{
int die3 = random.Next(1, 7);
dieThree.Content = die3;
rollsRemaining = rollsRemaining - 1;
string string1 = rollsRemaining.ToString();
rollsLeft.Text = "Rolls Left: " + string1;
roundScore.Text = "Round Score: " + (die1 + die2 + die3 + die4);
}
else
{ }
}
private void dieFour_Click(object sender, RoutedEventArgs e)
{
Random random = new Random();
if (rollsRemaining != 0)
{
int die4 = random.Next(1, 7);
dieFour.Content = die4;
rollsRemaining = rollsRemaining - 1;
string string1 = rollsRemaining.ToString();
rollsLeft.Text = "Rolls Left: " + string1;
roundScore.Text = "Round Score: " + (die1 + die2 + die3 + die4);
}
else
{ }
}
private void newRound_Click(object sender, RoutedEventArgs e)
{
scoreTotal = scoreTotal + scoreRound;
totalScore.Text = "Total Score: " + scoreTotal;
rollsRemaining = 6;
rollsLeft.Text = "Rolls Left: " + rollsRemaining;
scoreRound = 0;
roundScore.Text = "Round Score: " + scoreRound;
Random random = new Random();
int die1 = random.Next(1, 7);
int die2 = random.Next(1, 7);
int die3 = random.Next(1, 7);
int die4 = random.Next(1, 7);
dieOne.Content = die1;
dieTwo.Content = die2;
dieThree.Content = die3;
dieFour.Content = die4;
}
}
}
答案 0 :(得分:2)
你的方法中有骰子的局部变量(die1,die2 ......)以及具有相同名称的类数据成员(die1,die2 ......)
当你使用变量时,首先编译器在当前范围内查找(这是你的方法),然后只搜索外部范围(类)。
您使用局部变量初始化了constructure范围内的数字,因此数据成员仍为0。
单击按钮时,使用值设置另一个局部变量(例如die1),然后将其与其余变量一起添加。所以现在die1(本地)添加了die2,3,4数据成员(它们是0)。所以你得到的只是die1。在其他按钮中也一样。
在此处详细了解范围:
https://msdn.microsoft.com/en-us/library/ms973875.aspx
仅针对这种情况 - 删除局部变量并仅使用数据成员。
public MainWindow()
{
InitializeComponent();
Random random = new Random();
die1 = random.Next(1, 7);
die2 = random.Next(1, 7);
die3 = random.Next(1, 7);
die4 = random.Next(1, 7);
//...
}
同样在newRound_Click