帮助保存和加载 [英] Help saving and Loading

查看:81
本文介绍了帮助保存和加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  我需要有关保存和加载阵列的帮助.我尝试了几种在互联网上发现的不同方法,但是没有运气.还 我想知道如何为菜单执行错误循环,如果除1-7和q以外的其他值,则给用户一个错误.我有只能输入1位数字的地方

 

> 使用 系统;

> 使用 System.Collections.Generic;

> 使用 System.Linq;

> 使用 System.Text;

> 使用 System.IO;

> 命名空间 ConsoleApplication1

{

程序

静态 void Main(字符串 []参数)

字典< 字符串字符串>员工= new 词典< 字符串字符串>();

"Harpo" " 654-0987" );

"Groucho" " 555-1234" );

胶水" " 456-7890" );

"Zeppo" " 867-5309" );

奇科" " 445-6789" );

字符 MenuAnswer;

控制台 .ForegroundColor = ConsoleColor .白色;

开关(MenuAnswer)

保护套 '1':

休息;

保护套 '2':

休息;

保护套 '3':

休息;

保护套 '4':

休息;

保护套 '5':

             &nsp /span> sortEmployee(employees);

休息;

保护套 '6':

休息;

保护套 '7':

休息;

保护套 'q':

控制台 .WriteLine(结束程序" + 环境 .NewLine);

休息;

默认:

              b ;  控制台 .WriteLine(无效选择" + 环境 .NewLine);

休息;

同时(MenuAnswer!= 'q');

公共 静态 字符 Menu()

字符 userAnswer;

控制台 .WriteLine(" =============== ============================" );

控制台 .WriteLine(菜单" );

控制台 .WriteLine(输入1以审核所有员工" );

控制台 .WriteLine(输入2以找到员工" );

控制台 .WriteLine(输入3以添加员工" );

控制台 .WriteLine(输入4以删除员工" );

控制台 .WriteLine(输入5对记录进行排序" );

控制台 .WriteLine(输入6保存记录" );

控制台 .WriteLine(输入7以加载记录" );

控制台 .WriteLine("输入q退出:" + 环境 .NewLine);

控制台 .Write("输入选择:" );

尝试

转换 .ToChar(控制台 .ReadLine());

捕获

控制台 .ForegroundColor = ConsoleColor .红色;

控制台 .WriteLine(环境 .NewLine + 错误-菜单选择必须正好是1个字符"();

"0" ;

控制台 .ForegroundColor = ConsoleColor .白色;

返回 userAnswer;

公共 静态 void addEmployee(字典< 字符串字符串>员工)

控制台 .WriteLine("输入员工姓名:" );

字符串 employeeName = 控制台 .ReadLine();

控制台 .WriteLine("输入电话号码:" );

字符串 employeeNum = 控制台 .ReadLine();

控制台 .WriteLine(已添加." + 环境 .NewLine);

公共 静态 无效 deleteEmployee(字典< 字符串字符串>员工)

控制台 .WriteLine("输入员工姓名:" );

string employeeToDelete = Console.ReadLine();

 

            foreach (String employee in employees.Keys)

                if (employeeToDelete == employee)

                {

                    employees.Remove(employee);

                    Console.WriteLine(Environment.NewLine + employee + " has been deleted." + Environment.NewLine);

                    返回;

                }

 

            Console.WriteLine("Sorry, no employee by that name..." + Environment.NewLine);

        }

 

        public static void sortEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine(Environment.NewLine + "Sorted List of Employees" + Environment.NewLine);

 

            SortedDictionary<String, String> tempSortedEmployees = new SortedDictionary<String, String>(employees);

            employees = new Dictionary<string, string>(tempSortedEmployees);

 

            foreach (KeyValuePair<String, String> pair in employees)

                Console.WriteLine(pair.Key + " " + pair.Value);

        }

 

        public static void viewEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine(Environment.NewLine + "List of Employees" + Environment.NewLine);

 

            foreach (KeyValuePair<String, String> pair in employees)

                Console.WriteLine(pair.Key + " " + pair.Value);

        }

 

        public static void searchEmployee(Dictionary<String, String> employees)

        {

            Console.Write("Enter name of Employee: ");

            string searchEmployee = Console.ReadLine();

 

            foreach (String employee in employees.Keys)

                if (searchEmployee == employee)

                {

                    Console.WriteLine(Environment.NewLine + employee + " " + employees[employee] + Environment.NewLine);

                    返回;

                }

 

            Console.WriteLine("Sorry, no employee by that name..." + Environment.NewLine);

        }

        public static void saveEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine("Saving.");

            string saveEmployee = Console.ReadLine();

            FileStream save = new FileStream(@"C:\Users\Public\TestFolder\WriteLines.txt", FileMode.Create, FileAccess.Write);

            StreamWriter save2 = new StreamWriter(save);

            for (int i = 0; i < employees.Length; i++)

                save2.WriteLine(employees[i]);

            save2.Close();

            save.Close();

        }

        public static void loadEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine("loading.");

            string loadEmployee = Console.ReadLine();

           

 

 

                }

 

 

            }

 

        }

 

 

   

 

 

解决方案

Hi,

 

you should look at how objects can be serialized to a file or deserialized from a file: http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file

 

Create an Employee class that will hold all your employee related info. Store all your employees in some serializable collection and use that Dictionary only for lookups (because Dictionary does not support serialization).

 

-Tomi


 I need Help with the saving and loading of my array. I tried several different things I found on the internet but no luck. Also I was wondering how would I do an error loop for the menu, gives the user an error if anything other than 1-7 and q. I have it where only 1 digit can be entered

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            Dictionary<String, String> employees = new Dictionary<string, string>();

            employees.Add("Harpo", "654-0987");

            employees.Add("Groucho", "555-1234");

            employees.Add("Gummo", "456-7890");

            employees.Add("Zeppo", "867-5309");

            employees.Add("Chico", "445-6789");

 

            char MenuAnswer;

 

            Console.ForegroundColor = ConsoleColor.White;

 

            do

            {

                MenuAnswer = Menu();

 

                switch (MenuAnswer)

                {

                    case '1':

                        viewEmployee(employees);

                        break;

                    case '2':

                        searchEmployee(employees);

                        break;

                    case '3':

                        addEmployee(employees);

                        break;

                    case '4':

                        deleteEmployee(employees);

                        break;

                    case '5':

                        sortEmployee(employees);

                        break;

                    case '6':

                        saveEmployee(employees);

                        break;

                    case '7':

                        loadEmployee(employees);

                        break;

                    case 'q':

                        Console.WriteLine("Ending program" + Environment.NewLine);

                        break;

                    default:

                        Console.WriteLine("Invalid selection" + Environment.NewLine);

                        break;

                }

            }

            while (MenuAnswer != 'q');

        }

 

        public static char Menu()

        {

            char userAnswer;

 

            Console.WriteLine("===========================================");

            Console.WriteLine("Menu");

            Console.WriteLine("Enter 1 to veiw all Employees");

            Console.WriteLine("Enter 2 to find a Employee");

            Console.WriteLine("Enter 3 to add Employee");

            Console.WriteLine("Enter 4 to delete Employee");

            Console.WriteLine("Enter 5 to sort records");

            Console.WriteLine("Enter 6 to save records");

            Console.WriteLine("Enter 7 to load records");

            Console.WriteLine("Enter q to quit: " + Environment.NewLine);

            Console.Write("Enter selection: ");

 

            try

            {

                userAnswer = Convert.ToChar(Console.ReadLine());

            }

            catch

            {

                Console.ForegroundColor = ConsoleColor.Red;

 

                Console.WriteLine(Environment.NewLine + "Error - Menu Selection must be exactly 1 character");

                userAnswer = '0';

                Console.ForegroundColor = ConsoleColor.White;

            }

 

            return userAnswer;

        }

 

        public static void addEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine("Enter Employee Name: ");

            string employeeName = Console.ReadLine();

            Console.WriteLine("Enter Phone Number: ");

            string employeeNum = Console.ReadLine();

 

            employees.Add(employeeName, employeeNum);

            Console.WriteLine("Added." + Environment.NewLine);

        }

 

        public static void deleteEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine("Enter Employee Name: ");

            string employeeToDelete = Console.ReadLine();

 

            foreach (String employee in employees.Keys)

                if (employeeToDelete == employee)

                {

                    employees.Remove(employee);

                    Console.WriteLine(Environment.NewLine + employee + " has been deleted." + Environment.NewLine);

                    return;

                }

 

            Console.WriteLine("Sorry, no employee by that name..." + Environment.NewLine);

        }

 

        public static void sortEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine(Environment.NewLine + "Sorted List of Employees" + Environment.NewLine);

 

            SortedDictionary<String, String> tempSortedEmployees = new SortedDictionary<String, String>(employees);

            employees = new Dictionary<string, string>(tempSortedEmployees);

 

            foreach (KeyValuePair<String, String> pair in employees)

                Console.WriteLine(pair.Key + " " + pair.Value);

        }

 

        public static void viewEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine(Environment.NewLine + "List of Employees" + Environment.NewLine);

 

            foreach (KeyValuePair<String, String> pair in employees)

                Console.WriteLine(pair.Key + " " + pair.Value);

        }

 

        public static void searchEmployee(Dictionary<String, String> employees)

        {

            Console.Write("Enter name of Employee: ");

            string searchEmployee = Console.ReadLine();

 

            foreach (String employee in employees.Keys)

                if (searchEmployee == employee)

                {

                    Console.WriteLine(Environment.NewLine + employee + " " + employees[employee] + Environment.NewLine);

                    return;

                }

 

            Console.WriteLine("Sorry, no employee by that name..." + Environment.NewLine);

        }

        public static void saveEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine("Saving.");

            string saveEmployee = Console.ReadLine();

            FileStream save = new FileStream(@"C:\Users\Public\TestFolder\WriteLines.txt", FileMode.Create, FileAccess.Write);

            StreamWriter save2 = new StreamWriter(save);

            for (int i = 0; i < employees.Length; i++)

                save2.WriteLine(employees[i]);

            save2.Close();

            save.Close();

        }

        public static void loadEmployee(Dictionary<String, String> employees)

        {

            Console.WriteLine("loading.");

            string loadEmployee = Console.ReadLine();

           

 

 

                }

 

 

            }

 

        }

 

 

   

 

 

解决方案

Hi,

 

you should look at how objects can be serialized to a file or deserialized from a file: http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file

 

Create an Employee class that will hold all your employee related info. Store all your employees in some serializable collection and use that Dictionary only for lookups (because Dictionary does not support serialization).

 

-Tomi


这篇关于帮助保存和加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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