为什么无限循环中的 foreach 循环会一直持续下去?
Why does a foreach loop in infinite loop go on forever?
我正在尝试实现一个食谱管理器,用户可以在其中选择他们如何对每个食谱进行排序。根据用户的输入,我有一个 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
完成时应该已经坏了。
//var input = Console.ReadLine();
while (true)
{
var input = Console.ReadLine(); // put it here
switch (input)
{
@问,
您应该将 readLine 放在循环中,这样就会停止等待用户输入。
while (true)
{
Console.WriteLine(...);
var input = Console.ReadLine();
...
}
通过这样做,程序将始终显示一条消息并在打印任何其他内容之前要求输入。
我正在尝试实现一个食谱管理器,用户可以在其中选择他们如何对每个食谱进行排序。根据用户的输入,我有一个 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
完成时应该已经坏了。
//var input = Console.ReadLine();
while (true)
{
var input = Console.ReadLine(); // put it here
switch (input)
{
@问,
您应该将 readLine 放在循环中,这样就会停止等待用户输入。
while (true)
{
Console.WriteLine(...);
var input = Console.ReadLine();
...
}
通过这样做,程序将始终显示一条消息并在打印任何其他内容之前要求输入。