方法和循环.远超我的想象

本文关键字:我的 循环 方法 | 更新日期: 2023-09-27 18:15:38

所以我试图获得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. 一个雇员类的属性为fName, lName, title, badge将使这比使用2D数组更容易
  2. 创建员工数组=> Employee[] Employees = new Employee[20]
  3. 你的方法会变成简单的循环