锯齿状数组和异常

Jagged arrays and exceptions

另一个锯齿状数组问题:

目前在 class 而不是 Main() 中处理交错数组。有了这个锯齿状的数组,我想针对自定义异常测试我的用户输入。目前,当我 运行 程序时,它会创建数组,直到我要填充数组的最后一点,并针对我的异常测试该数据。尽管用户输入正确,但例外是 运行ning。我不确定错误是在我的异常中还是错误是我的数组。

注意:我知道其中一些内容可能看起来很复杂,但为了我正在做的事情,它必须保持这种状态。对不起:(

Class

class StudentGrades
{
    private char[][] grades;
    private double totalGpa;
    private int totalClasses;
    private int semesters;

    public StudentGrades ()
    {
        totalClasses = 0;
        SetSemesters(4);
        grades = new char [semesters][];
    }


    public void InputSemesters()
    {
        int x;
        int semestNum;
        bool check;
        do
        {
            check = false;
            try
            {
                for (int r = 0; r < grades.Length; r++)
                {
                    if (r == 0)
                    {
                        semestNum = 1;

                    }
                    else if (r == 1)
                    {
                        semestNum = 2;
                    }
                    else if (r == 2)
                    {
                        semestNum = 3;
                    }
                    else
                    {
                        semestNum = 4;
                    }
                    Console.Write("How many courses were taken semster {0}? ", semestNum);
                    x = int.Parse(Console.ReadLine());
                    CreateSemesters(r, x);  
                }
            }
            catch (System.FormatException e)
            {
                Console.WriteLine("Problem with input.\n{0}\nTry again.", e.Message);
                check = true;
            }
        } while (check);

    }

    public void CreateSemesters(int sem, int numClasses)
    {
        grades[sem] = new char[numClasses];
        totalClasses += numClasses;
    }

    public void EnterGrades()
    {
        int semestNum;
        char letter;
        bool check;
        do
        {
            check = false;
            try
            {
                for (int r = 0; r < grades.Length; r++)
                {
                    if (r == 0)
                    {
                        semestNum = 1;

                    }
                    else if (r == 1)
                    {
                        semestNum = 2;
                    }
                    else if (r == 2)
                    {
                        semestNum = 3;
                    }
                    else
                    {
                        semestNum = 4;
                    }
                    for (int c = 0; c < grades[r].Length; c++)
                    {

                        Console.Write("Enter the letter grade for class {0} of semester {1}: ", c + 1, semestNum); //the array creation works up to this part. So I know the array is creating but not storing data.
                        letter = char.Parse(Console.ReadLine());
                        letter = grades[r][c];
                        CheckLetterGrade(letter);
                    }
                }
            }
            catch (IncorrectLetterGradeException excepObj)
            {
                Console.Write("That is not an acceptable letter grade. Try Again. \n{0}", excepObj.Message);
                Console.WriteLine();
                check = true;
            }
        } while (check);
    }

    public void CheckLetterGrade(char G)
    {
        if (G != 'A' || G != 'B' || G != 'C' || G != 'D' || G != 'F')
        {
            IncorrectLetterGradeException excepObj = new IncorrectLetterGradeException("Not an acceptalbe letter grade of A-D or F");
            throw excepObj;
        }
    }

主要()

class UseStudentGrades
    {
        static void Main(string[] args)
        {
            StudentGrades student = new StudentGrades();
            // testing array functionality below
            student.InputSemesters();
            student.EnterGrades();
        }
    }

异常

class IncorrectLetterGradeException :
                System.ApplicationException
    {
        public IncorrectLetterGradeException(string exceptionType)
            : base (exceptionType)
        {
            //empty body
        }
    }

更新 1: 目前正在尝试此更改以查看异常是否停止提示:

public void CheckLetterGrade(char G)
{
    bool gradeMatch = (G == 'A' || G == 'B' || G == 'C' || G == 'D' || G == 'F');        
    if (!gradeMatch)
    {
        IncorrectLetterGradeException excepObj = new IncorrectLetterGradeException("Not an acceptalbe letter grade of A-D or F");
        throw excepObj;
    }
}

我遇到了这个问题:

Exception being thrown with correct user input

您的情况

if (G != 'A' || G != 'B' || G != 'C' || G != 'D' || G != 'F')

永远是真的。不管 G 中的字符是什么,它总是不等于其中一些字母。您应该使用 &&,逻辑 AND 运算符,而不是 || 逻辑 OR 运算符。

if (G != 'A' && G != 'B' && G != 'C' && G != 'D' && G != 'F')

这样,要抛出异常,G 中的字符必须不等于任何测试字母。

另请注意,您的比较区分大小写。在比较之前,您可能需要转换为大写:

char c = Char.ToUpper(G);
if (c != 'A' && c != 'B' && c != 'C' && c != 'D' && c != 'F')

您的代码中存在第二个问题,在本节中:

  letter = char.Parse(Console.ReadLine());
  letter = grades[r][c];
  CheckLetterGrade(letter);

这里你从你从用户输入解析的字符中分配字母。然后,您用从成绩数组中获得的值覆盖该值,从而完全丢失用户输入的值。然后检查从成绩中获得的价值(而不是从用户那里获得的价值)。我不知道你想用那条中间线做什么,所以我不确定要建议什么正确的代码,但这就是为什么用户条目没有真正得到检查的原因。我有一种感觉,虽然你真的想要这个:

  letter = char.Parse(Console.ReadLine());
  grades[r][c] = letter;
  CheckLetterGrade(letter);

正如已经指出的那样,问题在于复杂的 OR 语句。有几种方法可以改进代码,使其更具可读性。

下面测试G 是否有效,然后测试其否定。

public void CheckLetterGrade(char G)
{
    bool isValidGrade = (G == 'A' || G == 'B' || G == 'C' || G == 'D' || G == 'F');
    if (!isValidGrade)
    {
        IncorrectLetterGradeException excepObj = new IncorrectLetterGradeException("Not an acceptalbe letter grade of A-D or F");
        throw excepObj;
    }
}

接下来,如果我们想让有效成绩的测试更加简洁,我们可以这样做:

bool isValidGrade = "ABCDF".Contains(G);

在 C# 中,字符串是字符的集合,因此测试效果很好,并且可能更容易发现问题。