簡體   English   中英

鋸齒狀數組和異常

[英]Jagged arrays and exceptions

另一個鋸齒狀數組問題:

當前在類而不是Main()中具有鋸齒狀數組。 有了這個鋸齒狀的數組,我想針對自定義異常測試我的用戶輸入。 當前,當我運行程序時,它會創建數組,直到要填充數組的最后一點,然后針對我的異常測試該數據。 盡管用戶輸入正確,但異常正在運行。 我不確定錯誤是在我的異常中還是在我的數組中。

注意:我知道其中一些可能看起來很復雜,但是為了我正在做的事情,它必須保持這種狀態。 對不起:(

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;
    }
}

我得到這個問題:

使用正確的用戶輸入引發異常

你的情況

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

永遠是真的。 不管G的字符是什么,它總是不等於那些字母中的某些字母。 您應該使用&& ,而不是||邏輯AND運算符 邏輯或運算符。

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);

在這里,您可以根據從用戶輸入中解析出的字符來分配字母。 然后,您用從grades數組獲得的值覆蓋該值,從而完全丟失用戶輸入的值。 然后,您檢查從成績中獲得的價值(而不是從用戶那里獲得的價值)。 我不知道您要使用該中間線做什么,所以我不確定建議使用什么正確的代碼,但這就是為什么未真正檢查用戶輸入的原因。 我有一種感覺,盡管您真的想要這個:

  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#中,字符串是字符的集合,因此測試可以很好地進行,並且可能更容易發現問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM