在另一种方法中使用int

时间:2014-08-30 14:24:36

标签: c# visual-studio-2012 methods integer

嘿伙计我需要在Method Main中使用来自方法ValidateAns的Int调用点。我在网上搜索,人们说我应该做ValidateAns(积分),但它对我不起作用,我不确定我做错了还是我应该用其他方式来做它

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

2 个答案:

答案 0 :(得分:1)

我重组了你给我们的成员。

考虑命名约定,如果你要验证一些东西,你需要返回一个布尔值或bool来说明答案是否给定是否有效。见IsValidAnswer()。当编写返回bool的成员时,请考虑使用链接动词。是,有,意志。

当您比较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);
}

答案 1 :(得分:0)

将您的功能定义为

static int ValidateAns(int Nbr, char Ans)

并使用return points;

返回点值

然后用类似

的方式调用它
int p=ValidateAns(i + 1, Answear[i]);