C# StreamReader 从标签输入文件?

2023-12-08

我一直在使用StreamReader inputFile代码来自ListBox它效果很好,但是,我想输入来自.txt文件到一个Label盒子代替,这可能吗?这是我尝试过的代码,它给了我一个错误描述,说明

Use of unassigned local variable 'total'

 

private void Form1_Load(object sender, EventArgs e)
{
  try
  {
      int total = 0; 
      int highScore;
      StreamReader inputFile;
      inputFile = File.OpenText("HighScore.txt");
      while (!inputFile.EndOfStream)
      {
          highScore = int.Parse(inputFile.ReadLine());
          total += highScore;
      }
      inputFile.Close();
      highscoreLabel.Text = total.ToString("c");
  }
  catch (Exception ex)
  {
      MessageBox.Show(ex.Message);
  }
}

您看到的消息(“使用未分配的局部变量‘total’”)与“明确分配”相关,这将是以下场景:

int total; // note not yet assigned a value

...

total += {whatever}

但是,在您发布的代码中,它is明确分配(初始化为零)。因此,我怀疑错误消息被错误复制,或者代码示例不是失败案例的直接副本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# StreamReader 从标签输入文件? 的相关文章

随机推荐