我在宠物诊所代码方面遇到了麻烦,希望有人能为我提供一些帮助

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

嘿,我需要创建这个程序,要求用户输入宠物的名称,年龄,体重和类型。可用的两种类型是狗和猫。然后它将询问用户他们感兴趣的药物。有镇静剂和止痛药。无论您选择哪个选项,程序都应该应用数学公式来根据宠物的体重确切找到给您的宠物多少药物。您以磅为单位输入宠物的体重,但会将其转换为公斤。我也不完全确定petType的决策结构是否是字符串变量。我需要检查用户输入的是“狗”还是“猫”,并且我一直在使用if(petType.Contains(“ Dog”“ Cat”)== true),但这似乎正常了,但我我不确定到底每种宠物的药物使用量是否不同。我最麻烦的地方是应该为用户显示以ML为单位的金额,但是它是空白的,根本不显示任何内容。有任何想法吗?谢谢!PS:我是这个网站的新手,并认为我可以上传多少代码是有限制的。我将为您上传整个程序。抱歉!

static void Main(string[] args)
    {
        string petName = "";//string for the name of the pet
        int petAge = 0;//int for the pet's age
        double petWeight = 0;//double for the weight of the pet in pounds
        double petKiloWeight = 0;//double for the weight of the pet in kilograms
        string petType;//string for the pet's type (dog or cat)
        string userInput = "";//string for the users input when they are wanting to continue after entering their pet's info
        int userServiceMenuInput = 0;//int for the user's numbered choice when navigating the medication choices
        double AceproTotalAmount = 0;//double for the total amount of sedative the pet needs
        int AceproMGperML = 10;//int for the amount of MG per ML for sedative
        double AceproMGperKGforCat = 0.002;//double for the amount of MG per KG for a cat for sedative
        double AceproMGperKGforDog = 0.03;//double for the amount of MG per KG for a dog for sedative
        double CarprofTotalAmount = 0;//double for the total amount of painkiller the pet needs
        int CarprofMGperML = 12;//int for the amount of MG per ML for painkiller
        double CarprofMGperKGforCat = 0.25;//double for the amount of MG per KG for a cat for painkiller
        double CarprofMGperKGforDog = 0.5;//double for the amount of MG per KG for a dog for painkiller


        bool correctName = false;//bool to see if the correct name format has been entered by user
        bool correctAge = false;//bool to see if the correct age format has been entered by user
        bool correctWeight = false;//bool to see if the correct weight format has been entered by user
        bool correctPetType = false;//bool to see if the correct pet type format has been entered by user          
        bool containsLetter = Regex.IsMatch(petName, "[a-zA-Z]");//bool to see if the pet's name includes letters only
        bool vetActive = true;//bool that will loop as long as the vet program is active

        petKiloWeight = petWeight / 2.2046;

        CarprofTotalAmount = (petKiloWeight * CarprofMGperKGforCat) / (CarprofMGperML);
        CarprofTotalAmount = (petKiloWeight * CarprofMGperKGforDog) / (CarprofMGperML);

        AceproTotalAmount = (petKiloWeight * AceproMGperKGforCat) / (AceproMGperML);
        AceproTotalAmount = (petKiloWeight * AceproMGperKGforDog) / (AceproMGperML);

        Console.WriteLine("| ------------------------------------------------|");
        Console.WriteLine("|               CPSC1012 Pet Clinic               |");
        Console.WriteLine("|-------------------------------------------------|");

        while (vetActive == true)//main program loop
        {


            Console.WriteLine("Enter the name of your pet: ");//prompt for user to enter pet's name
            petName = Console.ReadLine();

            while (correctName == false)//while the correct name format is incorrect
            {
                if (containsLetter = Regex.IsMatch(petName, "[a-zA-Z]") == false)//if the pet's name doesn't include letters only
                {
                    correctName = false;
                    Console.WriteLine("Sorry! That isn't valid. The pet's name has to be made up of letters only!");//prompts user to enter the name again and will keep looping if it's incorrect
                    Console.WriteLine("Please enter your pet's name again: ");
                    petName = Console.ReadLine();

                }
                else
                {
                    correctName = true;//correct name format is correct and program continues
                }
            }

            Console.WriteLine("Enter the age in years of your pet: ");
            petAge = int.Parse(Console.ReadLine());

            while (correctAge == false)
            {
                if (petAge < 1)
                {
                    correctAge = false;
                    Console.WriteLine("Sorry! That isn't valid. The pet's age has to be at least 1 year!");
                    Console.WriteLine("Please enter the pet's age again: ");
                    petAge = int.Parse(Console.ReadLine());
                }
                else
                {
                    correctAge = true;
                }
            }

            Console.WriteLine("Enter the weight in pounds of your pet: ");
            petWeight = double.Parse(Console.ReadLine());

            while (correctWeight == false)
            {
                if (petWeight < 5)
                {
                    correctWeight = false;
                    Console.WriteLine("Sorry! That isn't valid. The pet's weight has to be at least 5 pounds!");
                    Console.WriteLine("Please enter your pet's weight again: ");
                    petWeight = double.Parse(Console.ReadLine());
                }
                else
                {
                    correctWeight = true;

                }
            }

            Console.WriteLine("Enter Dog or Cat: ");
            petType = Console.ReadLine();

            do
            {
                if (petType.Contains("Cat") == false || petType.Contains("Dog") == false)
                {
                    correctPetType = false;
                }
                else
                {
                    correctPetType = true;
                }

            } while (correctPetType == true);

            Console.WriteLine($"Name: {petName}, Age: {petAge}, Weight: {petWeight}, Type: {petType}");
            Console.WriteLine("Is the information above correct? Enter y or n: ");
            userInput = Console.ReadLine();
            vetActive = false;

            switch (userInput)
            {
                case "y":
                    ServiceMenu(userServiceMenuInput, petWeight, petType, petName, CarprofTotalAmount, AceproTotalAmount, petKiloWeight, CarprofMGperKGforCat, CarprofMGperKGforDog, CarprofMGperML);
                    break;

                case "n":
                    vetActive = true;
                    break;

                default:
                    Console.WriteLine("Sorry! That's invalid. Have a good day!");
                    return;
            }
        }                                         
    }
    static bool ServiceMenu(int userServiceMenuInput, double petWeight, string petType, string petName, double CarprofTotalAmount, double AceproTotalAmount, double petKiloWeight, double CarprofMGperKGforCat, double CarprofMGperKGforDog, int CarprofMGperML)
    {
        Console.WriteLine("Service Options");
        Console.WriteLine("1. Pain Killer");
        Console.WriteLine("2. Sedative");
        Console.WriteLine("3. Both Pain Killer & Sedative");
        Console.WriteLine("Enter the service (1-3) required for your pet: ");
        userServiceMenuInput = int.Parse(Console.ReadLine());
        switch (userServiceMenuInput)
        {
            case 1:
                petKiloWeight = petWeight / 2.2046;
                CarprofTotalAmount = (petKiloWeight * CarprofMGperKGforCat) / (CarprofMGperML);
                CarprofTotalAmount = (petKiloWeight * CarprofMGperKGforDog) / (CarprofMGperML);
                if (petType.Contains("Dog") == true)
                {
                    Console.WriteLine($"{petName} requires {CarprofTotalAmount}ml of Carprofen.");
                }
                if (petType.Contains("Cat") == true)
                {
                    Console.WriteLine($"{petName} requires {CarprofTotalAmount}ml of Carprofen.");
                }
                return true;
            case 2:
                Sedative(petType, petName, AceproTotalAmount);
                return true;
            case 3:
                PainSed(petType, petName, AceproTotalAmount, CarprofTotalAmount);
                return true;
            default:
                return false;

        }
    }   
    static void Sedative(string petType, string petName, double AceproTotalAmount)
    {
        if (petType.Contains("Dog"))
        {
            Console.WriteLine($"{petName} requires {AceproTotalAmount}ml of Acepromazine.");
        }
        if (petType.Contains("Cat"))
        {
            Console.WriteLine($"{petName} requires {AceproTotalAmount}ml of Acepromazine.");
        }
    }
    static void PainSed(string petType, string petName, double CarprofTotalAmount, double AceproTotalAmount)
    {
        if (petType.Contains("Dog"))
        {
            Console.WriteLine($"{petName} requires {AceproTotalAmount}ml of Acepromazine.");
            Console.WriteLine($"{petName} requires {CarprofTotalAmount}ml of Carprofen.");

        }
        if (petType.Contains("Cat"))
        {
            Console.WriteLine($"{petName} requires {AceproTotalAmount}ml of Acepromazine.");
            Console.WriteLine($"{petName} requires {CarprofTotalAmount}ml of Carprofen.");
        }
    }
}         
c#
1个回答
0
投票

正如评论中指出的那样,工作程序更容易进行故障排除。我已经放任自流,并稍加修改了您的代码,以消除重复的逻辑并允许更轻松的扩展(请参见SOLID)。希望这可以为您带来一些思考。

© www.soinside.com 2019 - 2024. All rights reserved.