方法和循环。超越我的头脑

时间:2016-10-30 20:55:43

标签: c# loops methods user-input

所以我试图获得这个GetValidString()方法。完成此方法后,其余代码应该很简单。

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


namespace ReviewEchProject
{
class Program
{
    //Arrays that can be called upon anytime
    static string[,] employeeInfo = new string[20, 3];  //Used to contain First Name, Last Name, and Title
    static int[] badgeArray = new int[20];  //Used to contain badge number


 //Main method will be used to call upon the other methods
static void Main(string[] args)
    {
        Console.WriteLine("Welcome to the Employee Data Base. Please Type       a letter to accept the command.");
        Console.WriteLine();
        Console.WriteLine("A = Add Employee  E = Edit Employee  S= Search     Employee  X= Exit");
        string userInput = Console.ReadLine().ToUpper();
        switch (userInput)
        {
            case "A":
                AddEmployee();
                break;
            case "E":
                EditEmployee();
                break;
            case "S":
                SearchEmployee();
                break;
            case "X":
                break;
            default:
                Console.WriteLine("Sorry, Invalid Input. Program will now shut down.");
                break;
        }
        Console.WriteLine();
        Console.WriteLine("Have a nice day!");
        Console.ReadLine();
    }



    public static void SearchEmployee()
    {
        Console.WriteLine("I am in the SearchEmployee method");
    }


    public static void EditEmployee()
    {
        Console.WriteLine("I am in the EditEmployee method");
    }


    public static void AddEmployee()
    {
        for (int i = 0; i < badgeArray.Length; i = i + 1)
        {
            if (badgeArray[i] != 0)
            {
                Console.WriteLine("ERROR: Database is full.");
            }


            if (badgeArray[i] == 0)
            {
                int badge = GetEmployeeBadgeNumber();
                badgeArray[i] = badge;
               //Used to create badge number


               //Not sure if done correctly, but call upon three times to place names for each slot
                string firstName = GetValidString();
                employeeInfo[i, 0] = firstName;


                string lastName = GetValidString();
                employeeInfo[i, 1] = lastName;


                string titleName = GetValidString();
                employeeInfo[i, 2] = titleName;


                Console.WriteLine("Database upload ... ... Success.");
            }
        }
    }

    public static int GetEmployeeBadgeNumber()
    {
       //Needs to return an int that is at least 100 an no greater than 999. 
       //This method asks for the value from the user.  
       //Warn the user about this constraint, and stay in a loop until the user gets it right.
        return 100;
    }


    public static string GetValidString()
    {
       //loop that it stays in until the user enters a valid entry, and when the user does enter a valid entry, it should return the value the user enters 
      //ask the user to enter a value with a message that uses the passed in messageString
     //warn the user the input string must be must be at least 3 letters and less than 20. verify the length. if its not acceptable, write error message and loop again
        return "Test";
    }
}
}

同样的冲突适用。我正在考虑使用GetValidString()方法,我应该使用while循环来确保它可以遍历所有内容。

**我被迫使用2D数组,所以我必须使用它。

1 个答案:

答案 0 :(得分:1)

这里有太多的代码要为你写。不过我会提出建议。

  1. 具有fName,lName,title,badge属性的Employee类将比使用2D数组更容易
  2. 创建一个Employees数组=&gt; Employee[] Employees = new Employee[20]
  3. 您的方法将转变为简单的循环