我正在尝试使用来自两个不同按钮的名为playerTallyTotals
的方法计算总计。但是不是计算它们并将其打印出来,而是打印出传递给它的第二个int的值。
private void playerTallyTotals(int playerCardTotalx)
{
int playerCardFinalTotal = 0;
playerCardFinalTotal = playerCardFinalTotal + playerCardTotalx;
textBox1.Text = playerCardFinalTotal.ToString();
}
答案 0 :(得分:0)
int playerCardFinalTotal = 0;
playerCardFinalTotal = playerCardFinalTotal + playerCardTotalx;
这相当于:
playerCardFinalTotal = 0 + playerCardTotalx;
或者
playerCardFinalTotal = playerCardTotalx;
但是不是计算它们并将其打印出来,而是打印出传递给它的第二个int的值。
此函数只传递了1个参数。听起来好像你的逻辑本身有点缺陷。
也许你应该有这种逻辑:
private void playerTallyTotals(int playerCardTotalx, int playerCardTotaly)
{
playerCardFinalTotal = playerCardTotaly + playerCardTotalx;
textBox1.Text = playerCardFinalTotal.ToString();
}
答案 1 :(得分:0)
将playerCardFinalTotal
移到方法之外。每次调用该方法时,它都会重新初始化(归零)。当超出方法范围时,playerCardFinalTotal
将适用于班级的生活(或在您的案例中)。
int playerCardFinalTotal = 0;
private void playerTallyTotals(int playerCardTotalx)
{
playerCardFinalTotal = playerCardFinalTotal + playerCardTotalx;
textBox1.Text = playerCardFinalTotal.ToString();
}