为什么无限循环中的foreach循环会永远持续下去?

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

我正在尝试实现一个配方管理器,用户可以选择如何对每个配方进行排序。根据用户的输入,我有一个foreach循环遍历Recipes列表中的每个项目,它应该遍历每个项目,并在必要时对它们进行排序。现在,我有一个无限循环,让我可以让用户在必要时输入输入,但是当我尝试查看列表时,它会一遍又一遍地反映出结果。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace recipeManager
{
    class Program
    {
        static List<recipes> Recipes = new List<recipes>()
        {
            new recipes() { recipeName = "Meatloaf", recipeType = "Dinner", listOfIngredients = new List<string> { "Ground beef", "Egg", "Onion", "Milk" } },
            new recipes() { recipeName = "PBnJ Sandwich", recipeType = "Lunch", listOfIngredients = new List<string> { "Peanut Butter", "Jelly", "Bread" } },
            new recipes() { recipeName = "Cake", recipeType = "Dessert", listOfIngredients = new List<string> { "Flour", "Sugar", "Baking Powder" } },
            new recipes() { recipeName = "Key lime pie", recipeType = "Dessert", listOfIngredients = new List<string> { "Key Lime Juice", "Egg Yolk", "Milk" } },
            new recipes() { recipeName = "Jambalaya", recipeType = "dinner", listOfIngredients = new List<string> { "Crawfish", "Egg Yolk", "Milk" } }
        };

        static void Main(string[] args)
        {

            Console.WriteLine("Hi there, how would you like to view our recipes? Enter View All for the");
            Console.WriteLine("entire recipe book, Sort Name to sort the recipes by name, Sort Type to sort");
            Console.WriteLine("the recipes by type, Sort Ingredients to sort by ingredients, break to break");
            Console.WriteLine("the loop");
            var input = Console.ReadLine();

            while (true)
            {
                switch (input)
                {
                    case "View All":
                        printAll();
                        break;
                    case "Sort Name":
                        sortName();
                        break;
                    case "Sort Ingredients":
                        sortIngredients();
                        break;
                    case "break":
                        return;
                    default:
                        Console.WriteLine("Not a valid command, please try again");
                        break;
                }
            }
        }

        //print entire list
        static void printAll()
        {
            foreach (recipes recipeProp in Recipes)
            {
                Console.WriteLine(recipeProp.recipeName + ", " + recipeProp.recipeType + ", " + "[" + String.Join(", ", recipeProp.listOfIngredients) + "]");
            }
        }

        //sort by name
        static void sortName()
        {
            var orderedNames = Recipes.OrderBy(l => l.recipeName);

            foreach (recipes recipeNames in orderedNames)
            {
                Console.WriteLine(recipeNames.recipeName + ", " + recipeNames.recipeType + ", " + "[" + String.Join(", ", recipeNames.listOfIngredients) + "]");
            }
        }

        //sort by type
        static void sortType()
        {
            var orderedType = Recipes.OrderBy(t => t.recipeType);

            foreach (recipes recipeTypes in orderedType)
            {
                Console.WriteLine(recipeTypes.recipeName + ", " + recipeTypes.recipeType + ", " + "[" + String.Join(", ", recipeTypes.listOfIngredients) + "]");
            }
        }

        static void sortIngredients()
        {

        }
    }
}

删除while工作,但我不明白为什么它会永远循环,因为foreach不会永远继续下去,我甚至有一个break声明,当foreach完成时应该已经打破。

c# loops infinite
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.