我正在尝试为当前分数添加分数,因此当我们打印输出时,我希望将之前的分数添加到+1,然后将其总结并打印新分数。这不起作用,我尝试了很多选项,我怎么能解决这个问题?我获得的输出是1,并且在第一个结果之后显示相同的输出。
namespace scoreboard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public double regnut()
{
int score = 0;
return score = score + 1;
}
private void button1_Click(object sender, EventArgs e)
{
string input = textBox1.Text;
int score;
count();
}
void count()
{
string iny = textBox1.Text;
double score = 0;
if (iny == "t")
{
score = score + 1;
listBox1.Items.Add(score);
}
label1.Text = "Score: " + score;
}
}
}
答案 0 :(得分:2)
这是因为您始终将score
初始化为零。您需要一个包含score
当前值的变量。示例如下:
private int currentScore = 0; // holds you current Score value
void count()
{
string iny = textBox1.Text;
int score = currentScore; // sets initial value based from the current Score
if (iny == "t")
{
score = score + 1; // increment value
listBox1.Items.Add(score);
}
currentScore = score; // store the value in the variable
label1.Text = "Score: " + currentScore.ToString();
}