简体   繁体   中英

Organising code into methods and classes

I made a program which has 5 questions, it reads the users answer and lets them know if they were correct or not, if they were incorrect the user is forced to begin the 5 questions again. I added a count increment so that the program can tell the user the number of times it took to complete the quiz and i also added a "questions left" left feature which will tell you how many questions were left. as of now the code is all in one class and not separated into methods and uses the old "go to" loops. How would I go about changing the loops to a more modern code and how would I program it to use more methods for organising it, also the questions left feature works if the user gets every question correct but when the user gets a question wrong and restarts the questions left feature doesn't output the correct number.

here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace practicePro
{
class Program
{
    public static void Main(string[] args)
    {

        /*----------------------------------------Declaration----------------------------------------- */

        string q1, q2, q3, q4, q5;
        int questionsLeft;
        questionsLeft = 5;
        /*----------------------------------------TITLE----------------------------------------- */

        Console.WriteLine("Welcome to the Ultimate quiz!");
        Console.WriteLine();

        /*----------------------------------------QUESTION 1----------------------------------------- */
        int count = 0;
    start:
        count++;
        Console.WriteLine("What programming language has a snake as its name" + "  questions left: " + questionsLeft );
        Console.WriteLine();
        q1 = Console.ReadLine();
        q1 = q1.ToUpper();

        if (q1 == "PYTHON")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
            questionsLeft--;
        }

        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;

        }

        Console.WriteLine(); 

        /*----------------------------------------QUESTION 2----------------------------------------- */

        Console.WriteLine("What is the age range to qualify for an apprenticeship in the uk? Please type in the following format xx-yy" + "  questions left: " + questionsLeft);
        Console.WriteLine();
        q2 = Console.ReadLine();

        if (q2 == "16-24")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
            questionsLeft--;
        }

        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
            count++;
        }

        Console.WriteLine();


        /*----------------------------------------QUESTION 3----------------------------------------- */

        Console.WriteLine("Is HTML a programming language (Yes or No)" + "  questions left: " + questionsLeft);
        Console.WriteLine();
        q3 = Console.ReadLine();
        q3 = q3.ToUpper();

        if (q3 == "NO")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
            questionsLeft--;
        }

        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
            count++;
        }

        Console.WriteLine();


        /*----------------------------------------QUESTION 4----------------------------------------- */
        Console.WriteLine("In JavaScript, What are the 2 charecters used to symbolise a single line comment?" + "  questions left: " + questionsLeft);
        Console.WriteLine();
        q4 = Console.ReadLine();

        if (q4 == "//")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
            questionsLeft--;
        }

        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
            count++;

        }

        Console.WriteLine();


        /*----------------------------------------QUESTION 5----------------------------------------- */
        Console.WriteLine("500 < 600 && 700 < 600");
        Console.WriteLine();
        Console.WriteLine("Is the above statement true or false ?" + "  questions left: " + questionsLeft);
        Console.WriteLine();
        q5 = Console.ReadLine();
        q5 = q5.ToUpper();

        if (q5 == "FALSE")
        {
            Console.WriteLine();
            Console.WriteLine("Well Done, you may move on to the next question");
            Console.WriteLine();
            Console.WriteLine("Congratulations You have passed the quiz!");
            questionsLeft--;
        }

        else
        {
            Console.WriteLine("Sorry you got the answer wrong, you have to start again");
            goto start;
        }

        Console.WriteLine();

        Console.WriteLine("you took " + count + " time(s) to complete the quiz");

        }
    }
}

You can create a Question class, which will contain a text (question), and the right answer.

In your Main method, you can create and initialize the list of questions :

List<Question> questions = new List<Question>() {
    new Question("What programming language has a snake as its name ?", "PYTHON"),
    new Question(...),
    ...
}

Then you can create your workflow algorithm. Example :

Console.WriteLine("Welcome to the Ultimate quiz!");
Console.WriteLine();

int count = 0;

while(questions.Count > 0) {
    Console.WriteLine(question.First().Text + " (question left: " + questions.Count + ")");
    string answer = Console.ReadLine();

    if (answer == questions.First().Answer)
    {
        Console.WriteLine();
        Console.WriteLine("Well Done, you may move on to the next question");
        questions.RemoveAt(0);
    }
    else
    {
        Console.WriteLine("Sorry you got the answer wrong, you have to start again");
        count++;
    }
}

 Console.WriteLine();
 Console.WriteLine("you took " + count + " time(s) to complete the quiz");

You can even create a Ask() method in the Question class, which will ask the question and analyze the answer (this method must take in parameter the number of question left, to display it).

Writing methods that contain logic is easy:

// Without extra method
class Program
{
    static void Main(string[] args)
    {
        int a = 1;
        int b = 2;
        int c = a + b;
    }
}

would turn into

// With extra method
class Program
{
    static void Main(string[] args)
    {
        int a = 1;
        int b = 2;
        int c = Add(a, b);
    }

    static int Add(int num1, num2)
    {
        return num1 + num2;
    }
}

Basics about methods:

  • They always have an access modifier (public or private, if none is given, it's private in C#
  • They always have a return type (in this case: int)
  • They always have a name (in this case: Add)
  • They can take parameters (in this case: num1 and num2)
  • If the return type is not void they always need a return statement in every code path

In order to loop until the player has answered every question correctly, you could make the asking method a bool type and as long as it returns false (which should happen every time the player answers wrong), a Player class calls the asking method.

// Quiz being the asking method
while(!Quiz())
{
    // wrongAnswers being the counter for failed attempts
    wrongAnswers++;
}
Console.WriteLine("Success!");

This will call the Quiz() method, which will then ask the player its 5 questions (which can, as Paul DS stated, be stored in a seperate class) and if it returns false (which means the player has answered one question wrong), it adds 1 to wrongAttempts , before calling it again. If it returns true, wrongAnswers is not incremented by 1 and "Success!" is shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM