日期:2014-05-18 浏览次数:21032 次
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace csharp011 { public enum gender { man, woman }; public enum marital_status { y, n }; /// <summary> /// 访问修饰符 struct 结构名 /// { /// 结构体; /// } /// </summary> //e.g: public struct personal { public string name; public gender sex; public int age; public marital_status ms; public string tel; public string address; }; class Program { static void Main(string[] args) { bool flag1 = false,flag2 = false; personal pers; Console.Write("请输入个人信息:\n姓名(值为string类型):\t"); pers.name = Console.ReadLine(); Console.Write("性别(值为enum gender类型):\t"); do { try { pers.sex = (gender)(Enum.Parse(typeof(gender), Console.ReadLine())); flag1 = true; } catch (System.Exception ex) { Console.WriteLine("{0},请重新输入!", ex.Message); flag1 = false; } } while (!flag1); Console.Write("年龄(值为int类型):\t"); pers.age = Int32.Parse(Console.ReadLine()); //pers.ms = (marital_status)(Enum.Parse(typeof(marital_status), Console.ReadLine())); Console.Write("婚姻状况(值为enum marital_status类型):\t"); do { try { pers.ms = (marital_status)(Enum.Parse(typeof(marital_status), Console.ReadLine())); flag2 = true; } catch (System.Exception ex) { Console.WriteLine("{0},请重新输入!", ex.Message); flag2 = false; } } while (!flag2); Console.Write("电话号码(值为string类型):\t"); pers.tel = Console.ReadLine(); Console.Write("家庭住址(值为string类型):\t"); pers.address = Console.ReadLine(); Console.WriteLine("输出个人信息:"); Console.WriteLine("姓名:\t"+pers.name); [color=#FF0000]//Console.WriteLine("性别:\t"+pers.sex); //输出由键盘接收的枚举出错,说是未赋值的字段,请问要怎么处理??[/color] Console.WriteLine("年龄:\t"+pers.age); [color=#FF0000]//Console.WriteLine("婚姻状况:{0}\t", pers.ms);[/color] Console.WriteLine("电话号码:\t"+pers.tel); Console.WriteLine("家庭地址:\t"+pers.address); Console.WriteLine("\n按任意键退出程序..."); Console.ReadKey(); } } }