【发布时间】:2018-07-17 03:29:23
【问题描述】:
我正在做一个类似控制台应用程序的地铁餐厅。它在很大程度上依赖于循环,并且有一种特定类型的循环我已经重复了很多次。
基本上,我会编写选项,然后将光标放在初始选项上。用户在光标悬停时按 Enter 的任何选项都是选中的选项。
这是代码的第一部分(它有点大,但这就是我在这里问的原因):
static int Row = 0; //To set the row of the cursor
static void Menu()
{
Console.WriteLine("Hi, welcome to Subway!"); //Line 0
Console.WriteLine(); //Line 1
//Bread Selection
string[] breadOptions =
{
"Hearty Italian", //Element 0
"9-Grain Honey Oat", //Element 1
"Jalapeno Cheese", //Element 2
"Monterey Cheddar", //Element 3
"Parmesan Oregano", //Element 4
"Roasted Garlic", //Element 5
"Rosemary & Sea Salt", //Element 6
"Rye Bread", //Element 7
"Gluten-Free Bread", //Element 8
};
Console.WriteLine("Choose you bread!"); //Line 2
Console.WriteLine("[ ]" + breadOptions[0]); //Line 3
Console.WriteLine("[ ]" + breadOptions[1]); //Line 4
Console.WriteLine("[ ]" + breadOptions[2]); //Line 5
Console.WriteLine("[ ]" + breadOptions[3]); //Line 6
Console.WriteLine("[ ]" + breadOptions[4]); //Line 7
Console.WriteLine("[ ]" + breadOptions[5]); //Line 8
Console.WriteLine("[ ]" + breadOptions[6]); //Line 9
Console.WriteLine("[ ]" + breadOptions[7]); //Line 10
Console.WriteLine("[ ]" + breadOptions[8]); //Line 11
Row = 3; //set the row to the first bread option
int currentBread = 0; //a couter to keep track of the bread
ConsoleKeyInfo breadKey = new ConsoleKeyInfo();
do
{ //Loop that goes with the cursor through the menu items
for (Row = 3; Row < breadOptions.Length + 3; Row++)
{
//Highlight the current bread with the cursor in the box
if (currentBread == Row - 3)
Console.SetCursorPosition(1, Row);
}
//reads the key
breadKey = Console.ReadKey();
switch (breadKey.Key)
{
case ConsoleKey.DownArrow:
//if the current item is the last one in the array, reset the counter.
if (currentBread == breadOptions.Length - 1)
//-1 because currentBread follows the array, and array starts at 0
//but the array lengh doensn't start at 0. 0 is an empty array
currentBread = 0;
//otherwise, add one to the counter
else
currentBread++;
break;
case ConsoleKey.UpArrow:
//if the user presses Up in the first option, it goes to the last
if (currentBread == 0)
currentBread = breadOptions.Length - 1;
//otherwise, remove one from the counter
else
currentBread--;
break;
}
}
//keep looping until the user presses enter
while (breadKey.Key != ConsoleKey.Enter);
Console.SetCursorPosition(0, 12); //set the cursor back to the next line after the last Console.WriteLine
//make a string to store the selected bread and make it be the selected bread
string bread = breadOptions[currentBread];
而且,这个do-while循环又重复了几次,这是另一个例子:
//Bread Size
string[] sizeOptions = { "6-inches", "Footlong"};
Console.WriteLine(); //Line 12
Console.WriteLine("Choose a bread size."); //Line 13
Console.WriteLine("[ ]" + sizeOptions[0]); //Line 14
Console.WriteLine("[ ]" + sizeOptions[1]); //Line 15
Row = 14;
int currentSize = 0;
ConsoleKeyInfo sizeKey = new ConsoleKeyInfo();
do
{
for (Row = 14; Row < sizeOptions.Length + 14; Row++)
{
if (currentSize == Row - 14)
Console.SetCursorPosition(1, Row);
}
sizeKey = Console.ReadKey();
switch (sizeKey.Key)
{
case ConsoleKey.DownArrow:
if (currentSize == sizeOptions.Length - 1)
currentSize = 0;
else
currentSize++;
break;
case ConsoleKey.UpArrow:
if (currentSize == 0)
currentSize = sizeOptions.Length - 1;
else
currentSize--;
break;
}
} while (sizeKey.Key != ConsoleKey.Enter);
Console.SetCursorPosition(0, 16);
string size = sizeOptions[currentSize];
我想知道是否有办法在每次需要选择选项时不编写此循环。
【问题讨论】:
-
我强烈推荐阅读这篇Design patterns and practices in .NET: the Strategy Pattern 由 Andras Nemes 撰写。这是关于如何使用接口摆脱 switch 或 if else 语句
-
@RubenSebastian 这将非常有用!
标签: c# loops console console-application do-while