C# 我的程序无法运行。我认为这可能是命名的问题,但我';我不确定。我该怎么办?

C# 我的程序无法运行。我认为这可能是命名的问题,但我';我不确定。我该怎么办?,c#,C#,我正在制作一个程序,它读取一个.txt文件,然后显示平均分数、高于平均分数和低于平均分数。没有语法错误,但似乎没有什么能让它工作。单击按钮不起任何作用 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syst

我正在制作一个程序,它读取一个.txt文件,然后显示平均分数、高于平均分数和低于平均分数。没有语法错误,但似乎没有什么能让它工作。单击按钮不起任何作用

这是我的密码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace The_Score_List_V2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void ReadScores(List<int> scoreList)
        {
            try
            {
                StreamReader inputFile = File.OpenText("TestScores.txt");

                while (!inputFile.EndOfStream)
                {
                    scoreList.Add(int.Parse(inputFile.ReadLine()));
                }

                inputFile.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DisplayScores(List<int> scoreList)
        {
            foreach (int score in scoreList)
            {
                this.scoreList.Items.Add(score);
            }
        }

        private double average(List<int> scoreList)
        {
            int total = 0;
            double average;

            foreach (int score in scoreList)
            {
                total += score;
            }

            average = (double)total / scoreList.Count;

            return average;
        }

        private int AboveAverage(List<int> scoreList)
        {
            int numAbove = 0;

            double avg = average(scoreList);

            foreach (int score in scoreList)
            {
                if (score > avg)
                {
                    numAbove++;
                }
            }

            return numAbove;
        }

        private int BelowAverage(List<int> scoreList)
        {
            int numBelow = 0;

            double avg = average(scoreList);

            foreach (int score in scoreList)
            {
                if (score < avg)
                {
                    numBelow++;
                }
            }

            return numBelow;
        }

        private void getScoresButton_Click(object sender, EventArgs e)
        {
            double averageScore;
            int numAboveAverage;
            int numBelowAverage;

            List<int> scoreList = new List<int>();

            ReadScores(scoreList);

            DisplayScores(scoreList);

            averageScore = average(scoreList);
            averageLabel.Text = averageScore.ToString("n1");

            numAboveAverage = AboveAverage(scoreList);
            this.numAboveAverage.Text = numAboveAverage.ToString();

            numBelowAverage = BelowAverage(scoreList);
            this.numBelowAverage.Text = numBelowAverage.ToString();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO;
命名_分数_列表_V2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
私有无效读取分数(列表分数列表)
{
尝试
{
StreamReader inputFile=File.OpenText(“TestScores.txt”);
而(!inputFile.EndOfStream)
{
添加(int.Parse(inputFile.ReadLine());
}
inputFile.Close();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
}
}
私有void显示分数(列表分数列表)
{
foreach(分数表中的整数分数)
{
这个.scoreList.Items.Add(分数);
}
}
私人双倍平均(列表分数列表)
{
int-total=0;
双倍平均;
foreach(分数表中的整数分数)
{
总分+=总分;
}
平均值=(双倍)总数/得分表。计数;
收益率平均值;
}
私人智力高于平均水平(列表分数列表)
{
int numAbove=0;
双平均值=平均值(分数表);
foreach(分数表中的整数分数)
{
如果(分数>平均值)
{
numAbove++;
}
}
返回numAbove;
}
私人积分(列表分数列表)
{
int numberlow=0;
双平均值=平均值(分数表);
foreach(分数表中的整数分数)
{
如果(分数<平均值)
{
numberlow++;
}
}
返回次数低;
}
私有void getScoresButton_单击(对象发送者,事件参数e)
{
双平均分;
国际努马博韦平均数;
整数平均数;
List scoreList=新列表();
阅读分数(分数表);
显示分数(分数表);
averageScore=平均值(分数表);
averageLabel.Text=averageScore.ToString(“n1”);
numAboveAverage=高于平均水平(分数表);
this.numAboveAverage.Text=numAboveAverage.ToString();
NumberLowAverage=BelowAverage(分数表);
this.numberLowAverage.Text=numberLowAverage.ToString();
}
私有无效退出按钮单击(对象发送者,事件参数e)
{
这个。关闭();
}
}
}

还有设计视图中的名称,表明这不是一个连接问题。我在这里做错了什么?

我测试了您的代码,它运行得很好——只要文件路径正确。我认为您的问题在于TestScores.txt文件不在运行代码的同一文件夹中

如果您这样做是为了学习,我建议您指定文件的完整路径。否则,您可能需要使用OpenFileDialog对象,以便在运行时指定文件。微软的开发者网络有一篇好文章


此外,如果您想将此用于生产,请考虑使用<强> int .TyPARSER()< <强> >方法,而不是<强> int()/<强>,这样您就可以处理文本文件中经常发现的不良数据。

< P>我测试了您的代码,它运行得很好——一旦我的文件路径正确。我认为您的问题在于TestScores.txt文件不在运行代码的同一文件夹中

如果您这样做是为了学习,我建议您指定文件的完整路径。否则,您可能需要使用OpenFileDialog对象,以便在运行时指定文件。微软的开发者网络有一篇好文章


此外,如果您想将此用于生产,请考虑使用<强> int .TyPARSER()/<强>方法,而不是<强> int()/<强>,这样您就可以处理文本文件中经常发现的不良数据。如果转到getScoresButton的events属性选项卡,单击事件是否设置为getScoresButton_click?您是手动键入单击事件还是通过事件属性分配它?或者双击“设计”视图中的按钮?您可能在

scoreList
中遇到范围问题。为了使代码更清晰,以便您知道您正在引用您想要的内容,您应该为变量指定不同的名称。我通过双击按钮添加了该按钮。您确定已将处理程序与控件关联吗?如果转到getScoresButton的events属性选项卡,单击事件是否设置为getScoresButton_click?您是手动键入单击事件还是通过事件属性分配它?或者双击“设计”视图中的按钮?您可能在
scoreList
中遇到范围问题。为了让你的代码更清晰,让你知道你在引用你想要的东西,你应该给你的变量起不同的名字。