【问题标题】:Why does a foreach loop in infinite loop go on forever?为什么无限循环中的foreach循环会永远持续下去?
【发布时间】:2018-10-09 20:02:20
【问题描述】:

我正在尝试实现一个食谱管理器,用户可以在其中选择如何对每个食谱进行排序。根据用户的输入,我有一个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 被打破时完成了。

【问题讨论】:

  • 你的 while(true){} 导致了无限循环
  • @AnthonyLiriano 我明白,我想要它。我的问题是为什么它会导致 foreach 循环永远持续下去。
  • break 不会停止while 循环,它只是终止该案例的代码块,
  • 但是return; 将停止循环(和程序)就好了。

标签: c# loops infinite


【解决方案1】:
//var input = Console.ReadLine();

while (true)
{
    var input = Console.ReadLine();  // put it here

    switch (input)
    {

【讨论】:

  • 哦,对了,因为它一直使用我最初的输入。完全错过了。
【解决方案2】:

@问,

您应该将 readLine 放在循环中,这样会停止等待用户输入。

   while (true)
   {
      Console.WriteLine(...);
      var input = Console.ReadLine();
      ...
   }

通过这样做,程序将始终显示一条消息并在打印其他任何内容之前要求输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 2017-08-19
    相关资源
    最近更新 更多