2017-05-05 148 views
0

另一個參差不齊的陣列問題:鋸齒陣列和異常

當前在類中工作鋸齒狀數組而不是在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; 
     } 
    } 

的Main()

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

+1

我敢肯定有可能縮減您的代碼到最低限度爲您的問題。誰應該讀這一切? –

+0

@ maf-soft這已被編輯刪除那些不屬於我的問題的項目。 – ZLackLuster

回答

0

你的條件

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

如果你有一個有效等級的「列表」,測試可以變得更易讀:'if(!validGrades.Contains(G))' –

+0

@BerinLoritsch - 甚至只是一個簡單的字符串'「ABCDF」'。我同意,有些事情可以改進。 – hatchet

+0

當我輸入A的等級時,還會得到例外提示嗎?也許我的條件措辭不正確?例外只應提示非A-D或F的信件。 – ZLackLuster

0

正如已經指出的,問題是複雜的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#中,字符串是字符的集合,使測試工作好吧,可能更容易發現問題。

+0

這個異常仍在拋出。我認爲這個錯誤存在於我的陣列中,但我不確定。 – ZLackLuster