【问题标题】:Using an int in another method在另一种方法中使用 int
【发布时间】:2014-08-30 14:24:35
【问题描述】:

大家好,我需要在方法 Main 中使用方法 ValidateAns 中的 Int 调用点。我在网上搜索,人们说我应该做 ValidateAns(points),但它对我不起作用,我不确定我是否做错了,或者我应该使用其他方法来做

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

【问题讨论】:

  • 您可以从ValidateAns 返回bool 以表明答案是否正确。在循环逻辑中,如果 bool 为真,您将向点数计数器加 1。
  • 我认为这个问题不应该有标签[visual-studio-2012]!

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


【解决方案1】:

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

考虑命名约定,如果要验证某些内容,则需要返回布尔值或布尔值来说明给出的答案是否有效。见IsValidAnswer()。在编写返回布尔值的成员时,请考虑使用连接动词。是、有、会。

当您比较类型char 时,您可以使用char.ToUpperInvariant(char val),因此您不必将您的答案与相同字符的不同情况进行比较。

我希望这会有所帮助,请查看代码中的 cmets。作为开发人员,您会喜欢其中的一块金块。 :) 祝你有美好的一天

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

【讨论】:

  • 这个答案很有帮助,谢谢。
【解决方案2】:

定义你的函数为

static int ValidateAns(int Nbr, char Ans)

并使用return points;返回积分值

然后用类似的东西调用它

int p=ValidateAns(i + 1, Answear[i]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 2017-10-30
    相关资源
    最近更新 更多