2016-04-02 12 views
-3

Anahtar deyimini yalnızca iki kez döngüleyebilirim. İkinci seferden sonra konsol, anahtar kutusundan hangi seçeneği seçtiğim önemli değil. Kodumu nasıl değiştirmeliyim, böylece kullanıcı durmaya karar verene kadar anahtar kasasını değiştirmeye devam edebilir mi?İfadenin çıktısından iki kez sonra while döngüsüne nasıl devam edilir?

Önerileriniz için teşekkür ederiz.

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

* iç * döngü –

+1

Sizin switch deyimi herhangi döngü –

cevap

1

bu (pseudocode) gibi bir şey

Sen idam istediğiniz kodu koymak gerekir
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

içinde yer almayan istiyorum gibi görünüyor Kodumu while döngüsüne koydum ve işe yaradı. Ancak, bool yerine int türünü kullandım. Çok teşekkür ederim. – Luzing