【问题标题】:C# Simple Console Application Enumeration IssuesC# 简单控制台应用程序枚举问题
【发布时间】:2013-05-11 06:05:45
【问题描述】:

我是 C# 新手,需要以下代码方面的帮助。

这个控制台应用程序应该在 5 个不同的对应数组中存储 5 个申请人的详细信息。最后,这应该允许我通过我在声明我的数组后创建的菜单来查看申请人的姓名。请注意,该计划尚未完成。

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

  namespace ConsoleApplication1
 {
 enum Qualifying {Diploma=1, Bachelor=2, Degree=3, Masters_Degree=4, PhD=5};
 class Program
 {
    static void Main(string[] args)
    {
        string[] ids = new string[5];
        string[] names = new string[5];
        string[] dobs = new string[5];
        string[] yoe = new string[5];
        string[] loq = new string[5];

        bool x = true;
        while (x)
        {
            Console.WriteLine("Please choose an option");
            Console.WriteLine("1.Add Applicants Details");
            Console.WriteLine("2.Display Applicants names");
            Console.WriteLine("3.Search for Applicant details");
            Console.WriteLine("4.Modify Applicant details");
            Console.WriteLine("5.Exit");
            int choice = Convert.ToInt32(Console.ReadLine());

            switch (choice)
            {
                case 1:
                    {
                        Console.WriteLine("Please fill in the following: ");
                        for (int i = 0; i < ids.Length; i++)
                        {
                            Console.WriteLine("Enter 5 ID Card noS");
                            string id = Console.ReadLine();

                            ids[i] = id;
                        }

                        for (int i = 0; i < names.Length; i++)
                        {
                            Console.WriteLine("Enter 5 Full Names");
                            string name = Console.ReadLine();

                            names[i] = name;
                        }

                        for (int i = 0; i < dobs.Length; i++)
                        {
                            Console.WriteLine("enter 5 Date of births");
                            string dob = Console.ReadLine();

                            dobs[i] = dob;
                        }

                        for (int i = 0; i < yoe.Length; i++)
                        {
                            Console.WriteLine("Enter 5 Years of experience");
                            string years = Console.ReadLine();

                            yoe[i] = years;
                        }

                            Console.WriteLine("Enter 5 Levels of Qualification, Choose from the list and press the number according to the qualification held:\n1=Diploma, 2=Bachelor, 3=Degree, 4=Masters Degree, 5=PhD");

                            for(int i=0; i<5; i++)
                            {
                                int num=Convert.ToInt32(Console.ReadLine());                              
                                {
                                if (num <= 4)
                                {
                                    loq[i] = (Qualifying)num;
                                }
                                else
                                    Console.WriteLine("Invalid input");
                                }
                            }
                        break;
                    }
                case 2:
                    {
                        for (int i = 0; i < names.Length; i++)
                        {
                            Console.WriteLine("The names of the students are " + names[i]);
                        }
                        break;
                    }
                case 3:
                    {
                        Console.WriteLine("Please enter the name of the applicant who you like to edit");
                    }
                    break;
                case 4:
                    {
                        Console.WriteLine("Input the applicants ID NUMBER for modification: ");
                        string id1 = Console.ReadLine();

                        //if (id1 == id)
                        //    Console.WriteLine("Full Name");
                        string name1 = Console.ReadLine();
                        Console.WriteLine("Date of birth");
                        string dob1 = Console.ReadLine();
                        Console.WriteLine("Years of experience");
                        int years1 = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Levels of Qualification, Choose from the list:\nDiploma, Bachelor, Degree, Master Degree, PhD");
                        string quals1 = Console.ReadLine();
                        break;
                    }

                case 5:
                    {
                        Console.WriteLine("You chose to quit. BYE =D");

                        x = false;
                        break;
                    }
            }
        }
    }
}
}

我在第 78 行有一个错误说明:

“无法将类型 'ConsoleApplication1.Qualifying' 隐式转换为 'string'”

任何人都可以提供任何建议以解决此问题。

谢谢。

卢克

【问题讨论】:

  • loq[i] = (Qualifying)num;

标签: c# console-application


【解决方案1】:
loq[i] = (Qualifying)num;

应该是

loq[i] = ((Qualifying)num).ToString();

loq 存储一个字符串类型,而您正尝试放入 Qualifying 的枚举。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2012-06-03
    • 2021-08-20
    • 1970-01-01
    • 2011-07-22
    相关资源
    最近更新 更多