当前位置:编程学习 > C#/ASP.NET >>

skier类中想要添加如年龄、性别 该怎么实现?

using System;
using System.Collections;
using System.Text;
//using Queue;
//using Icollection;

namespace SKier
{
    struct Skier
    {
        public Skier(string name)
        {
            myname = name;
           // yearsold = 0;
        }
        public string myname;
        public int yearsold;
    }
    class Program
    {
        static void Main(string[] args)
        {
            SkiLift lift = new SkiLift();
            lift.Run();
        }
    }

    public class SkiLift
    {
        public SkiLift()
        {
            _theLift = new Queue();
        }
        protected void DisplayStatus()
        {
            Console.WriteLine("there are current {0} skier on the lift,", _theLift.Count);
            if (_theLift.Count > 0)
            {
                Skier firstSkier = (Skier)_theLift.Peek();
                Console.WriteLine("the first skier will be {0}.", firstSkier.myname);
                Console.WriteLine("Skier on the lift:");
                Array skiers = _theLift.ToArray();//?
                foreach (Skier askier in skiers)
                {
                    Console.WriteLine("\t" + askier.myname+askier.yearsold);
                }
            }
        }
        protected SkiAction GetNextAction()
        {
            SkiAction result= SkiAction.Quit;
            bool done = false;

            while (done==false)
            {
                Console.WriteLine("A) Add a skier to the lift:");
                Console.WriteLine("R) del a skier to the lift");
                Console.WriteLine("Q) Quit");
                switch (Console.ReadLine().ToUpper())
                {
                    case "A":
                        result = SkiAction.AddSkier;
                        done = true;
                        break;

                    case "R":
                        result = SkiAction.RemoveSkier;
                        done = true;
                        break;
                    case "Q":
                        result = SkiAction.Quit;
                        done = true;
                        break;

                   // default: break;
                }
            }
            return result;
        }


        public void Run()
        {
            bool done = false;
            while (!done)
            {
                DisplayStatus();
                SkiAction choice = GetNextAction();
                switch (choice)
                {
                    case SkiAction.AddSkier:
                        string name;
                        int old;
                        do
                        {
                            Console.WriteLine("Skier is name:");
                            name = Console.ReadLine();
                            Console.WriteLine("how old are you:");
                           // old = Console.ReadLine();//怎么输入整形被读出到控制台?
                            
                        }
                        while (name.Length == 0);
                        Skier newSkier = new Skier(name);
                        _theLift.Enqueue(newSkier);
                        break;
                    case SkiAction.RemoveSkier:
                        if (_theLift.Count == 0)
                        {
                            Console.WriteLine("the lift is empty:");
                        }
                        else
                        {
                            Skier next = (Skier)_theLift.Dequeue();
                            Console.WriteLine("{0} has left the lift", next.myname);
                        }
                        break;

                    case SkiAction.Quit:
                        Console.WriteLine("Gookbye");
                        done = true;
                        break;
                    default:
                        break;
                }
            }
        }


        protected enum SkiAction { AddSkier, RemoveSkier, Quit };
        protected Queue _theLift;
    }
}

在这里的Queue类是不是要更换别的类HashTable?
有什么更好的解决办法吗?
--------------------编程问答-------------------- 没人吗  
自己顶下了 --------------------编程问答-------------------- 没有看懂 到底要干嘛啊 --------------------编程问答-------------------- 就是要在struct Skier 
    { 
        public Skier(string name) 
        { 
            myname = name; 
          // yearsold = 0; 
        } 
        public string myname; 
        //public int yearsold; 
    } 
这个类中多几个变量,要在输入时候有提示输入,和输出 
我现在输入的只有myname 我还想输入年龄,性别等
该怎么该?

另外一个问题:  switch (choice) 
                { 
                    case SkiAction.AddSkier: 
                        string name; 
                        int old; 
                        do 
                        { 
                            Console.WriteLine("Skier is name:"); 
                            name = Console.ReadLine(); 
                            Console.WriteLine("how old are you:"); 
                          // old = Console.ReadLine();//怎么输入整形被读出到控制台? 
                            
                        } 
这里我想读取整型变量 该怎么修改?

--------------------编程问答--------------------

struct Skier  //结构体~
{
       public Skier(string name, int age, bool gender)
        {
            _myname = name;
            _age = age;
            _gender = gender;            
        }
        private string _myname;
        private int _age;
        private bool _gender;
    } 


// old = Console.ReadLine();//怎么输入整形被读出到控制台? 

int age;
bool isValid = true;
do
{
   if(isValid )
     Console.WriteLine("how old are you:");
   else
     Console.WriteLine("Your input is invalid, how old are you:");
   isValid = false;
}while(!int.Tryparse(Console.ReadLine(), out age))

--------------------编程问答-------------------- --------------------编程问答-------------------- 结构体里也可以写set,get方法~ --------------------编程问答--------------------

struct student
    {
        public student(string inputName, int inputAge, bool inputGender)
        {
            _name = inputName;
            _age = inputAge;
            _gender = inputGender;
        }

        private string _name;
        public string name
        {
            set
            { _name = value; }
            get
            { return _name; }
        }

        private int _age;
        public int age
        {
            set
            {
                if (value <= 0 || value > 120)
                {
                    throw new Exception("the age you input is not valid");
                }
                else
                    _age = value;
            }
            get
            {
                return _age;
            }
        }

        private bool _gender;
        public bool gender
        {
            set
            { _gender = value; }
            get
            { return _gender; }
        }
    }
--------------------编程问答-------------------- 这里保存选手的时候还有个问题,提示:
A) Add a skier to the lift:
R) del a skier to the lift
Q) Quit
a
Skier is name:
ddsfs
how old are you:
13
there are current 2 skier on the lift,
the first skier will be sss.
Skier on the lift:
        sss0
        ddsfs0
应该是sss 12 
     ddsfs 13
才对!
是不是Queue不能保存这样的2维 要该成ashTable行不行?
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,