Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在另一个方法中使用int_C#_Visual Studio 2012_Methods_Integer - Fatal编程技术网

C# 在另一个方法中使用int

C# 在另一个方法中使用int,c#,visual-studio-2012,methods,integer,C#,Visual Studio 2012,Methods,Integer,嘿,伙计们,我需要在方法Main中使用一个名为points的Int,它来自于方法ValidateAns。我在网上搜索,人们说我应该做点什么,但是这对我来说不起作用,我不确定我是做错了还是应该用其他的方法来做 static void Main(string[] args) { const int QuestionNumbers = 10; char[] Answear = new char[QuestionNumbers]; Question[] MCQ = new Qu

嘿,伙计们,我需要在方法Main中使用一个名为points的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;
            }
    }
}
static void Main(字符串[]args)
{
常量整数=10;
char[]Answear=新字符[问题编号];
问题[]MCQ=新问题[问题编号];
MCQ[0]=新问题(屠宰场);
MCQ[1]=新问题(弗兰肯斯坦);
MCQ[2]=新问题(事物);
MCQ[3]=新问题(简);
MCQ[4]=新问题(Kill);
MCQ[5]=新问题(亲爱的);
MCQ[6]=新问题(盖茨比);
MCQ[7]=新问题(Catcher);
MCQ[8]=新问题(Pride);
MCQ[9]=新问题(十九);
for(int i=0;i
将您的函数定义为

static int ValidateAns(int Nbr, char Ans)
并使用
返回点返回点值

然后用这样的话来称呼它

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

我重组了你给我们的成员

考虑一下命名约定,如果要验证某个内容,则需要返回布尔值或布尔值,以说明给出的答案是否有效。请参阅
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);
}
private static void Main(字符串[]args)
{
常量整数=10;
var Answer=新字符[问题编号];
问题[]MCQ=新问题[问题编号];
MCQ[0]=新问题(屠宰场);
MCQ[1]=新问题(弗兰肯斯坦);
MCQ[2]=新问题(事物);
MCQ[3]=新问题(简);
MCQ[4]=新问题(Kill);
MCQ[5]=新问题(亲爱的);
MCQ[6]=新问题(盖茨比);
MCQ[7]=新问题(Catcher);
MCQ[8]=新问题(Pride);
MCQ[9]=新问题(十九);
for(int i=0;i
您可以从
ValidateAns
返回
bool
,以指示答案是否正确。在循环逻辑中,如果
bool
为真,您将向点数计数器添加1。我认为这个问题不应该有标签[visual-studio-2012]!答案很有用,谢谢。