示例#1
0
文件: Program.cs 项目: jnz1k/Examples
        static void Main()
        {
            string Continue;

            object   value  = DateTime.Now;
            DateTime result = (DateTime)value;
            double   Time   = double.Parse(result.ToString("HH.mm"));

            System.Console.WriteLine("The time is {0}", Time);

            Console.WriteLine();
            Console.WriteLine("Would you like to know how long it is to a spesific time today? (Y/N)");
            Continue = Console.ReadLine();

            while (Continue.ToLower() == "y")
            {
                if (Continue.ToLower() == "y")
                {
                    Console.WriteLine("What time would you like to count to? Format it like hh.mm in 24 hour time");

                    double CountTo = double.Parse(Console.ReadLine());
                    double NewTime = (CountTo - Time);

                    double Remainder = NewTime / 60;

                    string   split   = NewTime.ToString("00.00");
                    string[] parts   = split.Split('.');
                    int      Hours   = int.Parse(parts[0]);
                    int      Minutes = int.Parse(parts[1]);

                    if (Hours == 1)
                    {
                        Console.WriteLine("It is {0} hour and {1} minutes", Hours, Minutes);
                    }
                    else
                    {
                        Console.WriteLine("It is {0} hours and {1} minutes", Hours, Minutes);
                    }
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("Why are you here?");
                }
            }
        }
示例#2
0
        private static string RollAgain()
        {
            string Continue;

            Console.WriteLine();
            Console.WriteLine("Roll again?");
            Continue = Console.ReadLine();
            while (Continue.ToLower() != "yes" && Continue.ToLower() != "y" && Continue.ToLower() != "no" && Continue.ToLower() != "n")
            {
                Console.Write("Invalid entry. Try again! "); //input validation since there are only 2 right answers
                Continue = Console.ReadLine();
            }
            if (Continue == "no" || Continue == "n" || Continue == "N" || Continue == "No")
            {
                Environment.Exit(0);
            }
            Console.WriteLine();
            Console.WriteLine();
            return(Continue);
        }
        public static void Main()
        {
            Spell.RegisterSpell("Invisibility", typeof(Invisibility));
            Spell.RegisterSpell("Heal", typeof(Heal));
            Spell.RegisterSpell("Teleport", typeof(Teleport));

            SpellBook myspellbook        = new SpellBook();             //all available spells in a spellbook
            SpellBook saveAllCastedSpell = new SpellBook();             //spells that were cast and to save

            string choice;
            string Continue;
            Spell  s;
            string ObjectString;
            object objects = new object();

            //Adding available spells into spellbook
            myspellbook.AddSpells(new Teleport("1. Mitch's mighty mover <Teleport>"));
            myspellbook.AddSpells(new Heal("2. Paul's potent poultice <Heal>"));
            myspellbook.AddSpells(new Invisibility("3. David's dashing disapperance <Invisibility>"));
            myspellbook.AddSpells(new Teleport("4. Stan's stunning shifter <Teleport>"));
            myspellbook.AddSpells(new Heal("5. Lachlan's lavish longevity <Heal>"));

            //to convert list to an array because PrintSpellNamesspellname and PrintSpellNames take in parameter of an array , not list
            Spell[] spellbook = myspellbook.Spells.ToArray();

            ProgramInitialise(spellbook);

            do
            {
                choice = Console.ReadLine();
                switch (choice)
                {
                case "1":
                    s = myspellbook[0];
                    Console.WriteLine("Choose between cat and house\n");
                    ObjectString = Console.ReadLine();
                    objects      = CastOnObject(ObjectString.ToLower());
                    Console.WriteLine(s.Cast(objects));
                    saveAllCastedSpell.AddSpells(s);
                    break;

                case "2":
                    s = myspellbook[1];
                    Console.WriteLine("Choose between cat and house\n");
                    ObjectString = Console.ReadLine();
                    objects      = CastOnObject(ObjectString.ToLower());
                    Console.WriteLine(s.Cast(objects));
                    saveAllCastedSpell.AddSpells(s);
                    break;

                case "3":
                    s = myspellbook[2];
                    Console.WriteLine("Choose between cat and house\n");
                    ObjectString = Console.ReadLine();
                    objects      = CastOnObject(ObjectString.ToLower());
                    Console.WriteLine(s.Cast(objects));
                    saveAllCastedSpell.AddSpells(s);
                    break;

                case "4":
                    s = myspellbook[3];
                    Console.WriteLine("Choose between cat and house\n");
                    ObjectString = Console.ReadLine();
                    objects      = CastOnObject(ObjectString.ToLower());
                    Console.WriteLine(s.Cast(objects));
                    saveAllCastedSpell.AddSpells(s);
                    break;

                case "5":
                    s = myspellbook[4];
                    Console.WriteLine("Choose between cat and house\n");
                    ObjectString = Console.ReadLine();
                    objects      = CastOnObject(ObjectString.ToLower());
                    Console.WriteLine(s.Cast(objects));
                    saveAllCastedSpell.AddSpells(s);
                    break;

                case "6":
                    Console.WriteLine("Choose between cat and house\n");
                    ObjectString = Console.ReadLine();
                    objects      = CastOnObject(ObjectString.ToLower());
                    CastAllSpells(spellbook, objects);
                    foreach (Spell spell in spellbook)
                    {
                        saveAllCastedSpell.AddSpells(spell);
                    }
                    break;

                case "s":
                    saveAllCastedSpell.Save("Spell.txt");
                    break;

                case "c":
                    Console.Clear();
                    ProgramInitialise(spellbook);
                    break;

                case "l":
                    try
                    {
                        saveAllCastedSpell.Load("Spell.txt");
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Error loading file:{0}", e.Message);
                    }
                    break;

                case "q":
                    System.Environment.Exit(1);
                    break;

                default:
                    Console.WriteLine("Error , please try again");
                    break;
                }

                do
                {
                    Console.WriteLine("Do you want to continue? Yes or No \n");
                    Continue = Console.ReadLine();
                } while(Continue.ToLower() != "yes" && Continue.ToLower() != "no" && Continue.ToLower() != "y" && Continue.ToLower() != "n");
            } while(Continue.ToLower() == "yes" || Continue.ToLower() == "y");
            Console.WriteLine("See you again!!");
            Console.ReadKey();
        }