在我的测验结束时在C#中添加分数

时间:2017-10-08 18:24:04

标签: c# class methods

我正在尝试添加一种方法来为用户评分我的测验答案,并在测验结束时显示。试图找出一个简单的方法,我不必完全重做我当前的代码,因为我是一个业余编码器充其量。关于这个问题的任何建议将不胜感激。这是我当前代码的示例,以便您可以看到我正在使用的内容。我只包括前三个问题,但总共有10个问题:

using System;

namespace Quiz
{
class MultipleChoiceQuiz
{
    public static void CurrentQuestion(string correctAnswer)
    {
        do
        {
            string userAnswer = Console.ReadLine();
            if (userAnswer != "A" && userAnswer != "B" && userAnswer != "C" && userAnswer != "D")
            {
                Console.WriteLine("\nError - Not a Valid Input - Please Enter Valid Input");
            }
            else
            {
                if (userAnswer == correctAnswer)
                {
                    Console.WriteLine("\nThat is correct!");
                    break;
                }
                else if (userAnswer != correctAnswer)
                {
                    Console.WriteLine("\nSorry, that is incorrect.");
                    break;
                }
            }
        }
        while (true);
    }
    public static void Questions()
    {
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Please enter your first name: ");
        string firstName = Console.ReadLine();
        Console.WriteLine("\nWelcome to the HC-130J Power-On Quiz " + firstName + ".\n");
        Console.WriteLine("Using the keyboard, please submit answers by using the \'ENTER\' key.\n");
        Console.WriteLine("Please submit answers in CAPITAL letter form only.\n");
        Console.WriteLine("Ready to begin " + firstName + "? Hit the \'ENTER\' key now...");
        Console.ReadLine();
        Console.Clear();

        //Question 1
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 1 - What position does the ramp contol knob need to be in? " +
                          "\n\nA. 3N \nB. 1 \nC. 6N \nD. A or C \n\nWhat is your answer " + firstName + "?");
        CurrentQuestion("D");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

        //Question 2
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 2 - After power is applied to the aircraft, the battery needs to be turned off? " +
                          "\n\nA. True \nB. False \n\nWhat is your answer " + firstName + "?");
        CurrentQuestion("A");
        Console.Write("\n");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

        //Question 3
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 3 - Above what temperature does air condition need to be applied to the aircraft while power is applied? " +
                          "\n\nA. 75 degrees Fahrenheit \nB. 100 degrees Fahrenheit \nC. 95 degrees Fahrenheit \nD. 85 degrees Fahrenheit \n\nWhat is your answer " 
                          + firstName + "?");
        CurrentQuestion("C");
        Console.Write("\n");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

2 个答案:

答案 0 :(得分:0)

在类中添加一个静态int,如果在中断之前 CurrentQuestion 的答案是正确的,则添加一个静态int。

编辑:以下是一个例子:

using System;

namespace Quiz
{
class MultipleChoiceQuiz
{
    static int correct = 0; // this will hold the correct answers
    static int questions = 0; // this will hold the amount of questions

    public static void CurrentQuestion(string correctAnswer)
    {
        questions++;//this will add 1 when CurrentQuestion is called
        do
        {
            string userAnswer = Console.ReadLine();
            if (userAnswer != "A" && userAnswer != "B" && userAnswer != "C" && userAnswer != "D")
            {
                Console.WriteLine("\nError - Not a Valid Input - Please Enter Valid Input");
            }
            else
            {
                if (userAnswer == correctAnswer)
                {
                    Console.WriteLine("\nThat is correct!");
                    correct++;//this will add 1 to correct when you answer correctly
                    break;
                }
                else if (userAnswer != correctAnswer)
                {
                    Console.WriteLine("\nSorry, that is incorrect.");
                    break;
                }
            }
        }
        while (true);
    }
    public static void Questions()
    {
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Please enter your first name: ");
        string firstName = Console.ReadLine();
        Console.WriteLine("\nWelcome to the HC-130J Power-On Quiz " + firstName + ".\n");
        Console.WriteLine("Using the keyboard, please submit answers by using the \'ENTER\' key.\n");
        Console.WriteLine("Please submit answers in CAPITAL letter form only.\n");
        Console.WriteLine("Ready to begin " + firstName + "? Hit the \'ENTER\' key now...");
        Console.ReadLine();
        Console.Clear();

        //Question 1
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 1 - What position does the ramp contol knob need to be in? " +
                        "\n\nA. 3N \nB. 1 \nC. 6N \nD. A or C \n\nWhat is your answer " + firstName + "?");
        CurrentQuestion("D");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

        //Question 2
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 2 - After power is applied to the aircraft, the battery needs to be turned off? " +
                        "\n\nA. True \nB. False \n\nWhat is your answer " + firstName + "?");
        CurrentQuestion("A");
        Console.Write("\n");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

        //Question 3
        Console.WriteLine("Chad Mitchell - ENGR 115 - USAF HC130J Power On Quiz\n");
        Console.WriteLine("Question 3 - Above what temperature does air condition need to be applied to the aircraft while power is applied? " +
                        "\n\nA. 75 degrees Fahrenheit \nB. 100 degrees Fahrenheit \nC. 95 degrees Fahrenheit \nD. 85 degrees Fahrenheit \n\nWhat is your answer " 
                        + firstName + "?");
        CurrentQuestion("C");
        Console.Write("\n");
        Console.Write("\nPress \'ENTER\' to continue...");
        Console.ReadLine();
        Console.Clear();

        //Score
        Console.WriteLine("You got " + correct + " out of " + questions + " correctly!");
    }
}
}

答案 1 :(得分:0)

替换:

public static void CurrentQuestion(string correctAnswer)

public static int CurrentQuestion(string correctAnswer)

如果答案是正确的,则返回1,否则返回0。

score方法中添加Questions变量:

public static void Questions()
{
    int score = 0;

CurrentQuestion(Answer)来电更改为:

score += CurrentQuestion(...

最后显示score