将代码组织到方法和类中

时间:2015-11-16 15:34:40

标签: c#

我制作了一个有5个问题的程序,它会读取用户的答案并让他们知道他们是否正确,如果他们不正确,则用户被迫再次开始5个问题。我添加了一个计数增量,以便程序可以告诉用户完成测验所花费的次数,并且我还添加了一个"问题"左侧功能将告诉您剩下多少问题。截至目前,代码都在一个类中,并没有分成方法,而是使用旧的"转到"循环。我将如何将循环更改为更现代的代码以及如何对其进行编程以使用更多方法来组织它,如果用户得到的每个问题都是正确的,但是当用户得到错误的问题并重新启动时,问题左侧功能也会起作用留下的问题功能不会输出正确的数字。

这是代码:

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

        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以创建一个Question类,其中包含文本(问题)和正确答案。

Main方法中,您可以创建并初始化问题列表:

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

然后您可以创建工作流算法。示例:

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

您甚至可以在Ask()类中创建一个Question方法,该方法将询问问题并分析答案(此方法必须在参数中记录剩余的问题数量,以显示它)。

答案 1 :(得分:0)

编写包含逻辑的方法很简单:

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

会变成

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

方法基础:

  • 他们总是有一个访问修饰符(公共或私有,如果没有给出,它在C#中是私有的
  • 他们总是有一个返回类型(在这种情况下:int)
  • 他们总是有一个名字(在这种情况下:添加)
  • 他们可以获取参数(在本例中为:num1和num2)
  • 如果返回类型不是void,则在每个代码路径中始终需要一个return语句

为了循环直到玩家正确回答每个问题,你可以使ask方法成为bool类型,只要它返回false(每次玩家回答错误时都会发生),Player类会调用ask方法

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

这将调用Quiz()方法,然后向玩家询问其5个问题(如Paul DS所述,可以存储在单独的类中),如果它返回false(这意味着玩家有回答了一个错误的问题),在再次调用它之前,它向wrongAttempts添加1。如果返回true,则wrongAnswers不会递增1并显示"Success!"