我正在尝试实施一个配方管理器,用户可以选择如何对每个配方进行排序。根据用户的输入,我在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
完成后被打破。
答案 0 :(得分:4)
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy
ext = Extension("speed",
sources=["perf/speed.pyx"],
include_dirs=[numpy.get_include()],
language="c++",
libraries=[],
extra_link_args=[])
setup(ext_modules = cythonize([ext]))
答案 1 :(得分:0)
@Asking,
你应该把readLine放在循环中,这样就会停止等待用户输入。
while (true)
{
Console.WriteLine(...);
var input = Console.ReadLine();
...
}
通过这样做,程序将始终显示一条消息,并在打印任何其他内容之前要求输入。