我对如何在程序中使用方法和函数感到困惑... [英] Im confused about how I can use methods and functions to my program...

查看:60
本文介绍了我对如何在程序中使用方法和函数感到困惑...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我需要您的宝贵帮助,以便我完成编程任务.
我现在必须使用方法和函数,但是我不知道如何在程序中使用它们....

分配: http://pastebin.com/G7QNiPAc [ http://pastebin.com/SGT4uh4m [

Guys , I need your valuable help so I can complete my programming assignment.
I now that I have to use methods and functions but I dont know how to use them in the program....

Assignment: http://pastebin.com/G7QNiPAc[^] (Here are the instructions of what I have to do)

practest5: http://pastebin.com/SGT4uh4m[^] (This is the program that I have to make changes to)

and this is what I''ve done so far :

namespace Ass1
{
  public  class Arrays
    {

      static void Main(string[] args)
      {
           const int MAX_PEOPLE = 50;

            string[] people = new string[MAX_PEOPLE];

            string[] full_name = new string[MAX_PEOPLE];

            string[] adress = new string[MAX_PEOPLE];

            string[] street_number = new string[MAX_PEOPLE];

            string[] state = new string[MAX_PEOPLE];

            string[] phone_number = new string[MAX_PEOPLE];

            string[] post_code = new string[MAX_PEOPLE];

            float[] hours_worked = new float[MAX_PEOPLE];


            int current_people = 0;
            int total_people = 0;
            string tval = "";
            int c = 0;
            char choise = ' ';


            string vtemp = "";
            bool parseAttempt = true;
            string response = "";



            //Menu displayed
            while (response != "x" && response != "X")
            {
                Console.WriteLine("---------------What do you want to do ?---------------");
                Console.WriteLine("");
                Console.WriteLine("Add a person (a).");
                Console.WriteLine("Find a person(s).");
                Console.WriteLine("Display current person (p).");
                Console.WriteLine("Delete current person (d)");
                Console.WriteLine("Calculate and display pay slip for current person (c)");
                Console.WriteLine("");
                Console.WriteLine("Press 'x' to Exit. ");

                response = Console.ReadLine();

                switch (response)
                {

                    case "a":
                    case "A":

                        do
                        {
                            if (total_people < MAX_PEOPLE)
                            {
                                Console.Write("Enter person's full name >");
                                tval = Console.ReadLine();
                                full_name[current_people] = tval;

                                Console.Write("Enter person's Street number  >");
                                tval = Console.ReadLine();
                                street_number[current_people] = tval;

                                Console.Write("Enter person's Street adress  >");
                                tval = Console.ReadLine();
                                adress[current_people] = tval;

                                Console.Write("Enter person's State  >");
                                tval = Console.ReadLine();
                                state[current_people] = tval;

                                Console.Write("Enter person's Post code  >");
                                tval = Console.ReadLine();
                                post_code[current_people] = tval;

                                Console.Write("Enter person's phone number  >");
                                tval = Console.ReadLine();
                                phone_number[current_people] = tval;

                                Console.Write("Enter person's worked hours >");
                                tval = Console.ReadLine();
                                hours_worked[current_people] = float.Parse(tval);
                            }
                            else
                                Console.WriteLine("Person max {0} reached unable to store any more people", MAX_PEOPLE);

                            //Ask user to continue
                            Console.Write("Press 's'to continue or 'x' to exit  ");
                            response = string.Parse(Console.ReadLine());

                        } while (response == "s");
                        Console.Write("Enter the person to find >");
                        tval = Console.ReadLine();
                        for (c = 0; c < total_people; c++)
                        {
                            if (people[c] == tval)
                            {
                                current_people = c;
                                break;
                            }
                        }

推荐答案

不读作业(这是您的作业,我当然不会做),有几个您可以对代码做的事情,它将代码模块化并使其更易于阅读.

首先,您的代码是3个基本块.
1)初始化
2)循环
2.1)菜单写入和输入
2.2)遵守命令
3)Dunno-您的代码仅以中间方法结尾.

因此,我将其分为三个部分:
Without reading your assignment (and it''s your homework, I''m certainly not going to do it) there are a couple of things you can do to your code which will modularise it and make it easier to read.

Firstly, your code is 3 basic chunks.
1) Init
2) Loop
2.1) Menu write and input
2.2) Obey commands
3) Dunno - your code just ends in mid method.

So I would make it three chunks:
static void Main(string[] args)
   {
   Initialize();
   bool exit = false;
   while (!exit)
      {
      string response = GetUserInput();
      exit = ProcessUserInput(response);
      }
   }

现在您可以通过Main方法进行操作了,它简单易读,而且操作起来也不复杂.
请注意,我已经将您的字符串数组和其他变量移至Main之外-由于它们将被程序的其他方面引用,因此它们是类级别的,而不是方法局部变量,这是有道理的.

(目前)GetUserInput很简单:

Now you Main method is small, and easy to read - and it does nothing complex.
Note that I have moved your string arrays and other variables outside Main - since they will be referred to by other aspect of teh program, it makes sense that they are class level, not method local variables.

GetUserInput is (at present) simple:

private static string GetUserInput()
   {
   Console.WriteLine("---------------What do you want to do ?---------------");
   Console.WriteLine("");
   Console.WriteLine("Add a person (a).");
   Console.WriteLine("Find a person(s).");
   Console.WriteLine("Display current person (p).");
   Console.WriteLine("Delete current person (d)");
   Console.WriteLine("Calculate and display pay slip for current person (c)");
   Console.WriteLine("");
   Console.WriteLine("Press 'x' to Exit. ");
   return Console.ReadLine();
   }

再次,这现在很简单明了.
复杂的是您的ProcessUserInput:

Again, this is now simple, and clear.
The complex one is your ProcessUserInput:

private static bool ProcessUserInput(string input)
   {
   bool result = false;
   switch (input.ToLower())
      {
      default:
          UnknownInput(input);
          break;
      case "x":
          result = true;
          break;
      case 'a':
          AddPersons();
          break;
      ...
      }
   return result;
   }

同样,您可以看到每个阶段发生的事情,而且一切都很简单.
您将添加人员的复杂性转移到一个独立的方法中,让它为此担心.

在每个阶段,您都在使程序更简单,更易于阅读.

尝试一下,然后填写所需的方法.不要忘记它们也可以模块化!

祝你好运!

Again, you can see what is happening at each stage, and it is all simple.
You move the complexity of adding people to a self-contained method and let it worry about it.

At each stage, you are making the program less complex and easier to read.

Over to you to give it a try, and fill in the methods you need. Don''t forget that they can be modularised as well!

Good luck!


类,方法和函数都是 code-blocks ,通过使用它们,我们可以以更易于理解的方式管理代码.

方法-接受输入参数,执行任务
功能-接受输入参数,执行任务,返回输出参数
输入参数是可选的,如果需要的话可以声明

class, methods, function all are code-blocks, by using them we can manage our code in way it can be more easy to understand.

Methods - accept input parameter ,performs task
Function - accept input parameter ,performs task, returns output parameter
Input parameters are optional, if you want then declare

example:
1. you want to Fill data in controls while form load
then create Method 'FillData', and call it from load event (you can divide more if 'filldata' have lot of lines in code, like FillTreeView, FillComboes, FillTextBox... )

2. you want to Save data in database, it will need status that tell you data is stored or not?
then create Function 'SaveData' that return true/false value as status, and call it from Save buttons click, then check value if function returns true, show message "Saved Successfully" and Clear all Controls... else ...


这是一个示例,您可以通过许多不同的方式来管理同一件事

简而言之,以提高性能,可重用性,代码维护以及易于理解的方式管理代码块.

祝您编码愉快!
:)


it is an example, you can manage same thing in many different ways

In short manage code-blocks in way that improve performance, re-usability, code-maintenance and also easily-understandable.

Happy Coding!
:)


这篇关于我对如何在程序中使用方法和函数感到困惑...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆