2014-08-30 106 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]); 
相关问题