在继续下一个提示之前,有没有办法在我的控制台应用程序中返回错误

问题描述 投票:0回答:1

我基本上已经编写了整个程序,但我正在努力使其更清晰。在选项为 1、2、3 或 4 的动物选择菜单上,如果选择了另一个选项,我希望它立即返回错误。目前,如果选择了 5,系统会提示用户先输入一个名称,然后再返回一个有效选择的错误。我已经尝试自己重新排列代码,但最终还是无法正常工作或出现错误。非常感谢任何建议或帮助。

using System;

    abstract class Animal
    {
        public string Name { get; set; }
        public abstract void Speak();
    }

    class Dog : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The dog {Name} barks.");
        }
    }

    class Cat : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The cat {Name} meows.");
        }
    }

    class Pig : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The pig {Name} oinks.");
        }
    }

    class Cow : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The cow {Name} moos.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Animal[] animals = new Animal[100];
            int animalCount = 0;
            var menuChoice = "";

            while (menuChoice != "3")
            {
                Console.WriteLine("Select an option:");
                Console.WriteLine("1. Enter an animal");
                Console.WriteLine("2. Have all animals speak");
                Console.WriteLine("3. Exit");
                menuChoice = Console.ReadLine();

                switch (menuChoice)
                {
                    case "1":
                        if (animalCount >= 100)
                        {
                            Console.WriteLine("Maximum number of animals reached.");
                            break;
                        }

                        Console.WriteLine("Select an animal:");
                        Console.WriteLine("1. Dog");
                        Console.WriteLine("2. Cat");
                        Console.WriteLine("3. Pig");
                        Console.WriteLine("4. Cow");
                        var animalChoice = Console.ReadLine();

                        Console.WriteLine("Enter the desired animal's name:");
                        var animalName = Console.ReadLine();

                        Animal animal = null;
                        switch (animalChoice)
                        {
                            case "1":
                                animals[animalCount] = new Dog { Name = animalName };
                                animalCount++;
                                Console.WriteLine($"Added a dog named {animalName}.");
                                break;
                            case "2":
                                animals[animalCount] = new Cat { Name = animalName };
                                animalCount++;
                                Console.WriteLine($"Added a cat named {animalName}.");
                                break;
                            case "3":
                                animals[animalCount] = new Pig { Name = animalName };
                                animalCount++;
                                Console.WriteLine($"Added a pig named {animalName}.");
                                break;
                            case "4":
                                animals[animalCount] = new Cow { Name = animalName };
                                animalCount++;
                                Console.WriteLine($"Added a cow named {animalName}.");
                                break;
                            default:
                                Console.WriteLine("That was not a valid animal selection, please choose again from the menu above.");
                                continue;
                        }
                        if (animal != null)
                        {
                            animals[animalCount] = animal;
                            animalCount++;
                        }

                        break;
                    case "2":
                        if (animalCount == 0)
                        {
                            Console.WriteLine("There are currently no assigned animals that can speak.");
                        }
                        else
                        {
                            for (int i = 0; i < animalCount; i++)
                            {
                                animals[i].Speak();
                            }
                        }
                        break;
                    case "3":
                        Console.WriteLine("Thank you for using Animal Farm");
                        break;
                    default:
                        Console.WriteLine("Invalid selection, please choose an option from the menu above.");
                        break;
                }
            }
        }
    }
}

我尝试重新排列代码以及通过谷歌搜索错误。

c# console-application
1个回答
0
投票

复制了你的代码。首先,你有一个额外的右大括号。其次,jdweng 已经给出了应该做什么的说明。实施这可能没什么大不了的,但希望这会有所帮助。

namespace ConsoleApp1 // note: actual namespace depends on the project name
{
    abstract class Animal
    {
        public string Name { get; set; }
        public abstract void Speak();
    }

    class Dog : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The dog {Name} barks.");
        }
    }

    class Cat : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The cat {Name} meows.");
        }
    }

    class Pig : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The pig {Name} oinks.");
        }
    }

    class Cow : Animal
    {
        public override void Speak()
        {
            Console.WriteLine($"The cow {Name} moos.");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Animal[] animals = new Animal[100];
            int animalCount = 0;
            var menuChoice = "";

            while (menuChoice != "3")
            {
                Console.WriteLine("Select an option:");
                Console.WriteLine("1. Enter an animal");
                Console.WriteLine("2. Have all animals speak");
                Console.WriteLine("3. Exit");
                menuChoice = Console.ReadLine();

                switch (menuChoice)
                {
                    case "1":
                        if (animalCount >= 100)
                        {
                            Console.WriteLine("Maximum number of animals reached.");
                            break;
                        }

                        // enter animals name before its type
                        Console.WriteLine("Enter the desired animal's name:");
                        var animalName = Console.ReadLine();

                        // add bool variable which tells if animals type was valid, false by default
                        var validEntry = false;
                        Animal animal = null;

                        // while loop where you keep entering animals type until it was valid. When valid type is entered, validEntry becomes true and while loop is finished
                        while (!validEntry)
                        {
                            Console.WriteLine("Select an animal:");
                            Console.WriteLine("1. Dog");
                            Console.WriteLine("2. Cat");
                            Console.WriteLine("3. Pig");
                            Console.WriteLine("4. Cow");
                            var animalChoice = Console.ReadLine();

                            switch (animalChoice)
                            {
                                case "1":
                                    animals[animalCount] = new Dog { Name = animalName };
                                    animalCount++;
                                    Console.WriteLine($"Added a dog named {animalName}.");
                                    // thats where you need assign true to validEntry
                                    validEntry = true;
                                    break;
                                case "2":
                                    animals[animalCount] = new Cat { Name = animalName };
                                    animalCount++;
                                    Console.WriteLine($"Added a cat named {animalName}.");
                                    validEntry = true;
                                    break;
                                case "3":
                                    animals[animalCount] = new Pig { Name = animalName };
                                    animalCount++;
                                    Console.WriteLine($"Added a pig named {animalName}.");
                                    validEntry = true;
                                    break;
                                case "4":
                                    animals[animalCount] = new Cow { Name = animalName };
                                    animalCount++;
                                    Console.WriteLine($"Added a cow named {animalName}.");
                                    validEntry = true;
                                    break;
                                // if entry is invalid just print the error message and continue the while loop
                                default:
                                    Console.WriteLine("That was not a valid animal selection, please choose again from the menu above.");
                                    continue;
                            }
                        }
                        if (animal != null)
                        {
                            animals[animalCount] = animal;
                            animalCount++;
                        }
                        break;

                    case "2":
                        if (animalCount == 0)
                        {
                            Console.WriteLine("There are currently no assigned animals that can speak.");
                        }
                        else
                        {
                            for (int i = 0; i < animalCount; i++)
                            {
                                animals[i].Speak();
                            }
                        }
                        break;
                    case "3":
                        Console.WriteLine("Thank you for using Animal Farm");
                        break;
                    default:
                        Console.WriteLine("Invalid selection, please choose an option from the menu above.");
                        break;
                }
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.