【问题标题】:How To Fix while loop validation?如何修复while循环验证?
【发布时间】:2019-12-26 04:51:34
【问题描述】:

我想要它做什么:我正在尝试对其进行编码,以便最初询问用户是否有任何症状,并且用户回复输入,如果输入错误,他们会收到错误,否则,他们继续,然后选择他们的医疗状况,在他们选择了一个之后,他们可以选择另一个,如果是,它会循环,如果不是,它会跳出循环。

实际发生的情况:它可以很好地接收用户输入,但会错误地循环所有内容。

public static void userInput()
{
    Console.WriteLine("\n Which of these medical conditions do you have? Please type the corresponding number:" +
        "\n 1. headache \n 2. vomiting \n 3. cramp \n 4. rash \n 5. cough \n 6. fatigue \n 7. nausea \n 8. dizziness" +
        "\n 9. cold \n 10. chestPain \n 11. infection \n 12. flu \n 13. healthy \n 14. prescribeAntibiotics \n" +
        " 15. prescribePainkillers \n 16. gastroenteritis");

    bool loop = true;
    while(loop == true)
    {
        bool valid = false;
        while (valid == false)
        {
            string StringInput = Console.ReadLine();
            int input = 0;
            if (int.TryParse(StringInput, out input))
            {
                valid = true;
                bool done = false;
                while (done == false)
                {
                    Console.WriteLine("Thank you, Any other conditions? Y or N");
                    string temp = Console.ReadLine();
                    temp = Convert.ToString(temp);
                    switch (temp.ToUpper())
                    {
                        case "Y":
                            done = false;
                            loop = true;

                            break;
                        case "N":
                            done = true;
                            loop = false;
                            break;
                    }
                }                       
            }
            switch (input)
            {
                case 1:
                    FACTS["headache"] = true;
                    break;
                case 2:
                    FACTS["vomiting"] = true;
                    break;
                case 3:
                    FACTS["cramp"] = true;
                    break;
                case 4:
                    FACTS["rash"] = true;
                    break;
                case 5:
                    FACTS["cough"] = true;
                    break;
                case 6:
                    FACTS["fatigue"] = true;
                    break;
                case 7:
                    FACTS["nausea"] = true;
                    break;
                case 8:
                    FACTS["dizziness"] = true;
                    break;
                case 9:
                    FACTS["cold"] = true;
                    break;
                case 10:
                    FACTS["chestPain"] = true;
                    break;
                case 11:
                    FACTS["infection"] = true;
                    break;
                case 12:
                    FACTS["flu"] = true;
                    break;
                case 13:
                    FACTS["healthy"] = true;
                    break;
                case 14:
                    FACTS["prescribeAntibiotics"] = true;
                    break;
                case 15:
                    FACTS["prescribePainkillers"] = true;
                    break;
                case 16:
                    FACTS["gastroenteritis"] = true;
                    break;
                default:
                    Console.WriteLine("Sorry, please enter a number.");
                    break;
            }

        }                              
    }
}

【问题讨论】:

  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。
  • 我给的段落,是我打算让它做的,它实际上做的是错误循环并错误地跳出循环

标签: c# validation while-loop


【解决方案1】:

我认为你需要'done = true;' (不是假的)在“Y”和“N”两种情况下。否则你会卡在这个圈子里。如果你在'switch'语句之后要求新的条件会更好。因此,在 'while (done == false)' 循环之前将 'switch' 放在 'if (int.TryParse...' 条件中。

public static void userInput()
{
    var FACTS = new Dictionary<string, bool>();

    Console.WriteLine("\n Which of these medical conditions do you have? Please type the corresponding number:" +
        "\n 1. headache \n 2. vomiting \n 3. cramp \n 4. rash \n 5. cough \n 6. fatigue \n 7. nausea \n 8. dizziness" +
        "\n 9. cold \n 10. chestPain \n 11. infection \n 12. flu \n 13. healthy \n 14. prescribeAntibiotics \n" +
        " 15. prescribePainkillers \n 16. gastroenteritis");

    bool loop = true;
    while (loop == true)
    {
        bool valid = false;
        while (valid == false)
        {
            string StringInput = Console.ReadLine();
            int input = 0;
            if (int.TryParse(StringInput, out input))
            {
                valid = true;

                switch (input)
                {
                    case 1:
                        FACTS["headache"] = true;
                        break;
                    case 2:
                        FACTS["vomiting"] = true;
                        break;
                    case 3:
                        FACTS["cramp"] = true;
                        break;
                    case 4:
                        FACTS["rash"] = true;
                        break;
                    case 5:
                        FACTS["cough"] = true;
                        break;
                    case 6:
                        FACTS["fatigue"] = true;
                        break;
                    case 7:
                        FACTS["nausea"] = true;
                        break;
                    case 8:
                        FACTS["dizziness"] = true;
                        break;
                    case 9:
                        FACTS["cold"] = true;
                        break;
                    case 10:
                        FACTS["chestPain"] = true;
                        break;
                    case 11:
                        FACTS["infection"] = true;
                        break;
                    case 12:
                        FACTS["flu"] = true;
                        break;
                    case 13:
                        FACTS["healthy"] = true;
                        break;
                    case 14:
                        FACTS["prescribeAntibiotics"] = true;
                        break;
                    case 15:
                        FACTS["prescribePainkillers"] = true;
                        break;
                    case 16:
                        FACTS["gastroenteritis"] = true;
                        break;
                    default:
                        Console.WriteLine("Sorry, please enter a number.");
                        valid = false;
                        break;
                }
                if (valid == true)
                {
                    bool done = false;
                    while (done == false)
                    {
                        Console.WriteLine("Thank you, Any other conditions? Y or N");
                        string temp = Console.ReadLine();
                        temp = Convert.ToString(temp);
                        switch (temp.ToUpper())
                        {
                            case "Y":
                                done = true;
                                loop = true;

                                break;
                            case "N":
                                done = true;
                                loop = false;
                                break;
                        }
                    }
                }
            }
        }
    }
}

附:只是添加了一些小改动来处理输入错误号码的情况。

【讨论】:

    【解决方案2】:

    您是否考虑过尝试使用 ENUM 类?

    public enum Symptoms
            {
                HEADACHE =1,
                VOMITING =2,
                CRAMP =3,
                RASH =4,
                COUGH = 5,
                FATIGUE =6,
                NAUSEA =7,
                DIZZINESS =8,
                COLD =9,
                CHESTPAIN =10,
                INFECTION =11,
                FLU =12,
                HEALTHY =13,
                PRESCRIBEANTIBIOTICS =14,
                PRESCRIBEPAINKILLERS =15,
                GATROENTERITIS =16
            }
    

    您可以在您的流程中使用它。消除很多流程。

        public static void userInput()
            {
                while (true)
                {
    
                    Console.Clear();
                    Console.WriteLine("\n Which of these medical conditions do you have? Please type the corresponding number:" +
                        "\n 1. headache \n 2. vomiting \n 3. cramp \n 4. rash \n 5. cough \n 6. fatigue \n 7. nausea \n 8. dizziness" +
                        "\n 9. cold \n 10. chestPain \n 11. infection \n 12. flu \n 13. healthy \n 14. prescribeAntibiotics \n" +
                        " 15. prescribePainkillers \n 16. gastroenteritis ");
    
                    string StringInput = Console.ReadLine();
    
                    int.TryParse(StringInput, out int number);
    
                    if (number > 0 && number < 17)
                    {
                        // you can do other things as well here with the symptom
                        Symptoms symptom = (Symptoms)Enum.Parse(typeof(Symptoms), StringInput);
                        FACTS[symptom.ToString()] = true;
                    }
    
                    Console.WriteLine("Do you want to exit [N]?");
                    if (Console.ReadKey().Key == ConsoleKey.N)
                        break;
                }
            }
    

    您可以将您的 AddFacts 方法修复为:

        public static void addFACTS()
            {
                foreach (var x in Enum.GetValues(typeof(Symptoms)))
                    FACTS.Add(x.ToString(), false);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 2020-09-15
      • 2018-04-08
      • 1970-01-01
      • 2019-05-02
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多