-1
所以我一直在使用骰子博彩游戏(如果两个骰子超过7合起来,你赢了,否则你输了)。我得到的问题是我不知道如何让我的投注筹码减少或上升。我已经将筹码设置为100,如果你赢了,你会得到10筹码,如果输了,你会拿到10筹码。然而,在输赢的情况下,我的筹码会达到110或90.我怎么能这样做,以便我的程序将采取或添加10筹码到最新的筹码量,而不是原来的100.这是我现在的代码:C#WPF博彩游戏赢钱问题
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
#region dice throw
Random num = new Random();
int Number = new int();
int Number1 = new int();
int Chips = 100;
Number1 = num.Next(1, 7);
dice1.Text = Number1.ToString();
Number = num.Next(1, 7);
dice2.Text = Number.ToString();
#endregion
#region Win/lose
if (Number + Number1 >= 7)
{
if (bet10.IsChecked == true)
{
totalChips.Text = Chips.ToString();
Chips = Chips + 10;
}
if (bet20.IsChecked == true)
{
Chips = Chips + 20;
totalChips.Text = Chips.ToString();
}
if (bet50.IsChecked == true)
{
Chips = Chips + 50;
totalChips.Text = Chips.ToString();
}
winloseblock.Text = "You Win!";
}
else
{
if (bet10.IsChecked == true)
{
Chips = Chips - 10;
totalChips.Text = Chips.ToString();
}
if (bet20.IsChecked == true)
{
Chips = Chips - 20;
totalChips.Text = Chips.ToString();
}
if (bet50.IsChecked == true)
{
Chips = Chips - 50;
totalChips.Text = Chips.ToString();
}
winloseblock.Text = "You lose!";
}
#endregion
}
}
将'int Chips = 100;'移动到private void button_Click(object sender,RoutedEventArgs e)'上方的行。这是否有用? – mjwills
[当我调用方法时,变量重置为零]的可能重复(https://stackoverflow.com/questions/7744497/variables-reset-to-zero-when-i-call-a-method) – mjwills
听起来像您需要更好地了解范围和变量生命周期如何与变量一起工作。我将首先阅读关于该主题的更多内容。 –