Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

Switch statements for a Menu (c#)

I am currently doing an assignment for one of my programming units and I am required to use switch statements do display a menu and collect data. I seem to have worked out the switch statement itself, but I am wondering how I return the user to the menu once a case has executed?

For example :

Console.writeline("Enter Customer Details (s) "); 
Console.writeline("Enter usage data (u) "); 
Console.writeline("Display usage data (d) "); 

menu = console.readline();

switch (menu) 
 case 's':
 statement 
 break;

 case 'u':
 statement 
 break; 

 case 'd':
 statement 
 break; 

 default : 
 statement
 break;

Now, let's say the user wants to enter the Usage data first, how do I return them to the 'menu' so they can opt to enter customer details and/or display the data.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As I said in the comments, you could embed it in a while loop

bool continue = true;

while (continue)
{
    Console.WriteLine("Enter Customer Details (s)"); 
    Console.WriteLine("Enter usage data (u)"); 
    Console.WriteLine("Display usage data (d)");
    Console.WriteLine("Exit (e)");

    string menu = Console.ReadLine();

    switch (menu)
    {
        case "s":
            //statement 
            break;

        case "u":
            //statement 
            break; 

        case "d":
            //statement 
            break; 

        case "e":
        default: 
            continue = false;
            break;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...