2016-04-02 4 views
-3

Ich könnte die switch-Anweisung nur zweimal durchlaufen. Nach dem zweiten Mal schließt sich die Konsole, egal welche Option ich aus dem Schalterfall gewählt habe. Wie sollte ich meinen Code ändern, damit der Benutzer den Switch-Fall so lange wiederholen kann, bis er sich entscheidet aufzuhören?Wie wird die while-Schleife nach zweimaliger Ausgabe der Anweisung fortgesetzt?

Vielen Dank für Ihre Vorschläge.

int userChoice = 0; 

Console.WriteLine("This is an application. It has many different modes."); 
Console.WriteLine("You can activate the mode of the application, which can be inserting, retrieving, printing, saving or uploading."); 
Console.WriteLine("Type 'I' to insert, 'R' to retrieve, 'P' to print, 'S' to save, 'U' to upload the data, or 'T' to terminate the application."); 

string mode = Console.ReadLine(); 

switch (mode) 
{ 
    case "I": 
     Console.WriteLine("You have chosen to insert the data."); 
     break;       

    case "R": 
      Console.WriteLine("You have chosen to retrieve the data."); 
      break; 

    case "P": 
     Console.WriteLine("You have chosen to print the data."); 
     break; 

    case "S": 
     Console.WriteLine("You have chosen to save the data."); 
     break; 

    case "U": 
     Console.WriteLine("You have chosen to upload the data."); 
     break; 

    case "T": 
     Console.WriteLine("You have chosen to terminate the application.");      
     return;       
} 

// Two modes, quit option 
if (mode == "I" || mode == "R") 
{ 
    Console.WriteLine("The application will stay in the insert or retrieve modes. Press 'Q' to exit to the modes to return to the main menu."); 
} 

while (userChoice != 0) 
{ 
    Console.WriteLine("Please choose one of the modes {0}.", userChoice);     
      userChoice++; 
} 

Console.WriteLine("Type 'I' to insert, 'R' to retrieve, 'P' to print, 'S' to save, 'U' to upload the data, or 'T' to terminate the application."); 
Console.ReadLine(); 
+2

benötigen Sie den Code ausgeführt werden soll, setzen * innen * die Schleife –

+1

Ihre switch-Anweisung in einer Schleife ist nicht –

Antwort

1

Es sieht aus wie Sie so etwas wie dieses (Pseudo-Code) wollen

bool finished 
while(!finished) //Loop until they make a choice 
{ 
    Console.WriteLine("Type 'I' to insert, 'R' to retrieve, 'P' to print, 'S' to save, 'U' to upload the data, or 'T' to terminate the application."); 
    var input = Console.Readline(); 
    switch(input) 
    { 
     //Respond to the input. If the input should cause the application 
     //to terminate, set finished = true 
    } 

    if(!finished) 
    { 
     //Indicate that the user can make more choices 
    } 
} 

// The loop exits when finished = true, and the program ends 
+0

enthalten Ich habe meinen Code in die While-Schleife eingefügt und es hat funktioniert. Allerdings habe ich Int-Typ anstelle von Bool verwendet. Vielen Dank. – Luzing

Verwandte Themen