2017-09-14 76 views
-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 
    } 
} 
+0

將'int Chips = 100;'移動到private void button_Click(object sender,RoutedEventArgs e)'上方的行。這是否有用? – mjwills

+1

[當我調用方法時,變量重置爲零]的可能重複(https://stackoverflow.com/questions/7744497/variables-reset-to-zero-when-i-call-a-method) – mjwills

+0

聽起來像您需要更好地瞭解範圍和變量生命週期如何與變量一起工作。我將首先閱讀關於該主題的更多內容。 –

回答

0

要初始化的變量Chips每次單擊該按鈕:

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; 

    ... 
} 

你應該通過移動這個方法之外的聲明使Chips類的領域:

int Chips = 100;  

private void button_Click(object sender, RoutedEventArgs e) 
{ 
    #region dice throw 
    Random num = new Random(); 
    int Number = new int(); 
    int Number1 = new int(); 

    ... 
}