2014-08-30 91 views
1

嘿,我需要使用一個Int方法Main中的方法ValidateAns中的一個叫做點的方法。我搜索周圍的網和人說我應該做的ValidateAns(點),但它不爲我工作,林不知道如果我做錯了,或者我應該使用一些其他的方式來做到這一點在另一種方法中使用int

static void Main(string[] args) 
{ 
    const int QuestionNumbers = 10; 
    char[] Answear = new char[QuestionNumbers]; 

    Question[] MCQ = new Question[QuestionNumbers]; 

    MCQ[0] = new Question(Slaughterhouse); 
    MCQ[1] = new Question(Frankenstein); 
    MCQ[2] = new Question(Things); 
    MCQ[3] = new Question(Jane); 
    MCQ[4] = new Question(Kill); 
    MCQ[5] = new Question(Beloved); 
    MCQ[6] = new Question(Gatsby); 
    MCQ[7] = new Question(Catcher); 
    MCQ[8] = new Question(Pride); 
    MCQ[9] = new Question(Nineteen); 

    for (int i = 0; i < QuestionNumbers; i++) 
    { 
     Console.WriteLine("Question {0}", i + 1); 
     Answear[i] = MCQ[i](); 
     ValidateAns(i + 1, Answear[i]); 
     Console.WriteLine(); 
     Console.ReadKey(); 
    } 

} 

static void ValidateAns(int Nbr, char Ans) 
{ 
    int points= 0; 

    switch (Nbr) 
    { 
     case 1: 

      if (Ans == 'b' || Ans == 'B') 
      { 
       Console.WriteLine("Good Answear"); 
       points++; 
       break; 
      } 
      else 
      { 
       Console.WriteLine("Wrong Answer - The right answer was (B)"); 
       break; 
      } 
    } 
} 
+0

你可以從'ValidateAns'返回'bool'來表示答案是否正確。在循環邏輯中,如果「bool」爲true,則將向點的計數器加1。 – SimpleVar 2014-08-30 14:30:05

+0

我覺得這個問題不應該有標籤[visual-studio-2012]! – 2014-08-30 16:15:48

回答

1

我重組了你給我們的成員。

想一想命名約定,如果你要驗證你需要返回一個布爾值或布爾值來表明答案是否有效或無效。請參閱IsValidAnswer()。當編寫返回一個布爾的成員想到使用鏈接動詞。是,有,會。

當你比較類型char你可以使用char.ToUpperInvariant(char val),所以你不必比較你的答案太不同的情況下相同的字符。

我希望這可以幫助,看看代碼中的評論。那裏有一塊金塊,你將作爲開發者而熱愛。 :)祝你有個美好的一天

private static void Main(string[] args) 
{ 
    const int QuestionNumbers = 10; 
    var Answer = new char[QuestionNumbers]; 

    Question[] MCQ = new Question[QuestionNumbers]; 


    MCQ[0] = new Question(Slaughterhouse); 
    MCQ[1] = new Question(Frankenstein); 
    MCQ[2] = new Question(Things); 
    MCQ[3] = new Question(Jane); 
    MCQ[4] = new Question(Kill); 
    MCQ[5] = new Question(Beloved); 
    MCQ[6] = new Question(Gatsby); 
    MCQ[7] = new Question(Catcher); 
    MCQ[8] = new Question(Pride); 
    MCQ[9] = new Question(Nineteen); 

    for (int i = 0; i < QuestionNumbers; i++) 
    { 
     Console.WriteLine("Question {0}", i + 1); 
     Answer[i] = MCQ[i](); 

     // return bool since you want to validate an answer. 
     var result = IsValidAnswer(i + 1, Answer[i]); 

          // this is an if/else conditional statment, its called a ternary expression 
     Console.WriteLine(result ? "Answer is valid" : "Answer is not valid"); 
     Console.WriteLine(); 
     Console.ReadKey(); 
    } 
} 


private static bool IsValidAnswer(int Nbr, char Ans) 
{ 
    // if you really wanted to use a method. 
    var correctAnswer = default(char); 
    switch (Nbr) 
    { 
     case 1: 
      correctAnswer = 'b'; 
      break; 
     case 2: 
      //.. 
      break; 
    } 
    return char.ToUpperInvariant(Ans) == char.ToUpperInvariant(correctAnswer); 
} 
+0

答案很有幫助,謝謝。 – Skretek112 2014-08-31 12:51:46

0

定義功能

static int ValidateAns(int Nbr, char Ans) 

return points;

返回點值然後以某種稱呼它

int p=ValidateAns(i + 1, Answear[i]); 
相關問題