带有开关的菜单,列表和循环? [英] Menu with switch , List and loop?

查看:73
本文介绍了带有开关的菜单,列表和循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我对c#还是很陌生,我正在菜单上工作,我想在菜单中使用SWITCH/Case,将项目添加到列表,并为此程序循环.这就是我现在所在的位置:  ;

So i'm pretty new to c# and im working on a menu where i wanna use SWITCH / Case for the menu, items to a to a list and a loop for this program.. this is where i am now : 

class Program
{
    static void Main(string[] args)
    {                       
        Console.Title = "5";
        Console.ForegroundColor = ConsoleColor.Blue;
        // ________________________________________________________
        loop:
        Console.WriteLine("\n \t This is your bag!");
        Console.WriteLine("\t [1] to pack things");
        Console.WriteLine("\t [2] to pack things in the outercompartment");
        Console.WriteLine("\t [3] to see packed things");
        Console.WriteLine("\t [4] to quit");
        Console.WriteLine("\t your choice: ");
        string str = Console.ReadLine();
        int nr = Convert.ToInt32(str);
        List<string> items = new List<string>();
        items.Add(str);

        switch (nr)
        {
            case 1:
                packing:
                Console.Write("What would you like to pack? [QUIT for menu]\t");
                str = Console.ReadLine();
                if (str=="QUIT") goto loop;
                items.add(str);
                Console.WriteLine("You packed a " + str);
                goto packing;
                break;
            case 4:
                goto quitloop;

        }

        Console.ReadKey();
        goto loop;
    }

而我的问题是我如何以及在何处放置循环代码?需要所有帮助!

And my problem is how and where i put the loop code for this? All help is needed!

推荐答案

错!转到!移相器会被杀死!

Arrgh! Gotos! Phasers on kill!

处理此类循环的常用方法(无论是在外部询问您是否要在外层隔间或其他地方打包,还是在使用那些可怕的goto的内层循环)都带有"; while".像这样.

The usual way to handle these kind of loops (both the outer one where you ask if they want to pack or pack in the outer compartment or whatever, and the inner one where you are using those horrible gotos) is with a "while". Something like so.

string option = "";
while(option != "quit")
{
   // Code to display options goes here.

   // Get the user's choice.
   option = Console.ReadLine()

   // Act on the choice here. If they typed "quit", don't do anything, 
   // just let the code fall through to the end of the "while" statement
   // (that is, the ending brace (curly bracket)).
}


这篇关于带有开关的菜单,列表和循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆