c# 我有一个非常令人困惑的 InvalidCastException 错误

发布于 2025-01-18 01:55:11 字数 2915 浏览 2 评论 0原文

每当我尝试在要制作的程序中打开一种特定表格时,我的错误表示“无效的铸造异常”。这里的代码从文本文件中获取数据将其放入数据网格视图中。它还进行了计算,以将平均值放在数据网格视图中。我遇到的错误似乎涉及一个称为“ Skitime”的变量,该变量平均是其他3个变量:“ Skitime1”“ Skitime2”和“ Skitime3”。 或54.32等。

例如23.22

decimal skiTimeDecimal = Convert.ToDecimal(skiTime[i]);

这些数字是小数为小数, 由于我的代码如此杂乱,我基本上只是从随机的教程中撤出,以及我在这里提出的问题的答案,因为我需要为学校完成,而不知道C#。这是我认为可能与此错误有关的代码的完整部分。

 using (StreamWriter sw = new StreamWriter("groups.txt", false))
            {
                for (int i = 0; i < skiTimeID.Count; i++)
                {
                    for (int j = 0; j < quizID.Count; j++)
                    {
                        for (int l = 0; l < pupilDetailsID.Count; l++)
                        {
                            if (skiTimeID[i] == quizID[j] && quizID[j] == pupilDetailsID[l])
                            {
                                string fullPupilRecord = pupilDetailsID[l] + "~" + pupilDetailsFirstName[l] + "~" + pupilDetailsSurname[l] + "~" + pupilDetailsClass[l]
                                + "~" + pupilDetailsNumber[l] + "~" + skiTime[i] + "~" + quizScore[j] + "~" + quizPercentage[j];



                                int quizScoreNumber = Convert.ToInt32(quizScore[j]);
                                decimal skiTimeDecimal = Convert.ToDecimal(skiTime[i]);

                                if (quizScoreNumber >= 3 && skiTimeDecimal <= 40)
                                {
                                    groupLevel = "Advanced";
                                }

                                else if (quizScoreNumber >= 2 && skiTimeDecimal <= 50)
                                {
                                    groupLevel = "Intermediate";
                                }

                                else
                                {
                                    groupLevel = "Beginner";
                                }

                                fullPupilRecord = fullPupilRecord + "~" + groupLevel;
                                sw.WriteLine(fullPupilRecord);
                                fullPupilRecord = "";
                            }
                        }
                    }
                }

如果有帮助,这里是找到滑雪段变量之间的平均值的代码:

public string CalculateAverage(List<string> skiTime1, List<string> skiTime2, List<string> skiTime3)
    {
        List<string> allValues = new List<string>();
        allValues.AddRange(skiTime1);
        allValues.AddRange(skiTime2);
        allValues.AddRange(skiTime3);
        decimal totalcount = 0;
        decimal average = 0;

        foreach (var value in allValues)
        {
            totalcount = totalcount + decimal.Parse(value);
        }

        average = totalcount / allValues.Count();
        return average.ToString();
    }

I'm having an error that says "invalid cast exception" whenever I try to open one specific form in a program i'm making. The code here takes data from text files to put it into a data grid view. It also makes calculations to put averages in the data grid view. I'm getting an error that seems to involve a variable called "skiTime", which is an average of 3 other variables: "skiTime1" "skiTime2" and "skiTime3". The numbers are decimals, e.g. 23.22, or 54.32, etc.

When I try to load this form, the program closes and Visual studio highlights this line:

decimal skiTimeDecimal = Convert.ToDecimal(skiTime[i]);

I don't know what part of the code the error is in, it could literally be anywhere since my code is so messy, I basically just pulled from random tutorials, and answers to questions I asked here because I need it done for school and don't know c#. Here's the full part of the code that I think might relate to this error.

 using (StreamWriter sw = new StreamWriter("groups.txt", false))
            {
                for (int i = 0; i < skiTimeID.Count; i++)
                {
                    for (int j = 0; j < quizID.Count; j++)
                    {
                        for (int l = 0; l < pupilDetailsID.Count; l++)
                        {
                            if (skiTimeID[i] == quizID[j] && quizID[j] == pupilDetailsID[l])
                            {
                                string fullPupilRecord = pupilDetailsID[l] + "~" + pupilDetailsFirstName[l] + "~" + pupilDetailsSurname[l] + "~" + pupilDetailsClass[l]
                                + "~" + pupilDetailsNumber[l] + "~" + skiTime[i] + "~" + quizScore[j] + "~" + quizPercentage[j];



                                int quizScoreNumber = Convert.ToInt32(quizScore[j]);
                                decimal skiTimeDecimal = Convert.ToDecimal(skiTime[i]);

                                if (quizScoreNumber >= 3 && skiTimeDecimal <= 40)
                                {
                                    groupLevel = "Advanced";
                                }

                                else if (quizScoreNumber >= 2 && skiTimeDecimal <= 50)
                                {
                                    groupLevel = "Intermediate";
                                }

                                else
                                {
                                    groupLevel = "Beginner";
                                }

                                fullPupilRecord = fullPupilRecord + "~" + groupLevel;
                                sw.WriteLine(fullPupilRecord);
                                fullPupilRecord = "";
                            }
                        }
                    }
                }

If it helps, here is the code that finds the average between the variables for skiTime:

public string CalculateAverage(List<string> skiTime1, List<string> skiTime2, List<string> skiTime3)
    {
        List<string> allValues = new List<string>();
        allValues.AddRange(skiTime1);
        allValues.AddRange(skiTime2);
        allValues.AddRange(skiTime3);
        decimal totalcount = 0;
        decimal average = 0;

        foreach (var value in allValues)
        {
            totalcount = totalcount + decimal.Parse(value);
        }

        average = totalcount / allValues.Count();
        return average.ToString();
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文